Python3+Django2配置后台管理

栏目: Python · 发布时间: 6年前

内容简介:使用 Django 我们只需要做一些配置,就可以实现简单的后台管理系统,下面我们以新闻系统为例子来搭建后台。切换到工作空间,执行以下命令:

Python3+Django2配置后台管理

前言

使用 Django 我们只需要做一些配置,就可以实现简单的后台管理系统,下面我们以新闻系统为例子来搭建后台。

创建项目

切换到工作空间,执行以下命令:

django-admin.py startproject itstyle
# 进入 itstyle 文件夹
cd itstyle
# 创建 news App
manage.py startapp news

项目结构:

  manage.py
├─news
    admin.py
    apps.py
    models.py
    tests.py
    views.py
    __init__.py
  
  ├─migrations
      __init__.py
    
└─itstyle
      settings.py
      urls.py
      wsgi.py
      __init__.py

配置后台

修改 news 文件夹中的 models.py

# coding:utf-8
from django.db import models


class News(models.Model):
    title = models.CharField(u'标题', max_length=256)
    content = models.TextField(u'内容')

    create_time = models.DateTimeField(u'发布时间', auto_now_add=True, editable = True)
    update_time = models.DateTimeField(u'更新时间',auto_now=True, null=True)

把 news 加入到settings.py中的INSTALLED_APPS中

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'news',
)

把 settings.py中 DATABASES 修改数据源为MySql

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'itstyle',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'POST': '3306',
    }
}

同步所有的数据表

# 进入包含有 manage.py 的文件夹
manage.py makemigrations
manage.py migrate

创建管理员账号

manage.py createsuperuser

操作如下

E:\python3\Day10\itstyle>manage.py createsuperuser
Username (leave blank to use 'zzp'): admin
Email address: 345849402@qq.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

修改 admin.py

进入 news 文件夹,修改 admin.py 文件

from django.contrib import admin
from .models import News


admin.site.register(News)

最后,启动服务

manage.py runserver

访问 http://localhost :8000/admin/ 输入设定的帐号和密码,我们添加两篇新闻。

Python3+Django2配置后台管理

Python3+Django2配置后台管理


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Just My Type

Just My Type

Simon Garfield / Profile Books / 2010-10-21 / GBP 14.99

What's your type? Suddenly everyone's obsessed with fonts. Whether you're enraged by Ikea's Verdanagate, want to know what the Beach Boys have in common with easy Jet or why it's okay to like Comic Sa......一起来看看 《Just My Type》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具