Python3+Django2配置后台管理

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

内容简介:使用 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配置后台管理


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

查看所有标签

猜你喜欢:

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

The Smashing Book

The Smashing Book

Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90

The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

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

URL 编码/解码