基于django快速开发一个网站(一)

基于django快速开发一个网站(一)

*  创建虚拟环境、基于虚拟环境创建django==2.0.0和图片加载库和mysql数据库驱动

1. 创建目录并创建虚拟环境

╰$ mkdir Cornucopiavirtualenv
╰$ cd Cornucopiavirtualenv
╰$ virtualenv env1

2. 进入bin目录,并激活虚拟环境

╰$ cd bin
╰$ source activate

3. 进入环境、创建django和Pillow、PyMySQL

╰$ pip install django==2.0.
╰$ pip install pillow
╰$ pip install pymysql
或者也可以
╰$ pip install pymysql pillow django==2.0.

4. 创建Cornucopia的项目并设置book模块,并设置中文和中国上海的时区

╰$ django-admin startproject Cornucopia
╰$ python3 manage.py startapp book

4-1. 设置中文和中国上海的时区(在settings.py中修改)

LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'

5. 创建model模型数据

class User(models.Model):
nickname = models.CharField(max_length=50, blank=True)
email = models.EmailField('email address', blank=True, unique=True) def __str__(self):
return self.nickname class Category(models.Model):
name = models.CharField('分类名称', max_length=128) def __str__(self):
return self.name class Meta:
verbose_name = '书籍分类'
verbose_name_plural = verbose_name class Tag(models.Model):
name = models.CharField('标签', max_length=128) def __str__(self):
return self.name class Meta:
verbose_name = '书籍标签'
verbose_name_plural = verbose_name class Entry(models.Model):
title = models.CharField('文章标题', max_length=128)
author = models.ForeignKey(User, verbose_name='作者', on_delete=models.CASCADE)
book_imge = models.ImageField(upload_to='blog_img', null=True, blank=True, verbose_name='书籍配图')
# body = models.TextField('正文', )
body = MDTextField()
# abstract = models.TextField('摘要', max_length=256, null=True, blank=True)
abstract = MDTextField()
visiting = models.PositiveIntegerField('访问量', default=0)
category = models.ManyToManyField('Category', verbose_name='博客分类')
tags = models.ManyToManyField('Tag', verbose_name='标签')
created_time = models.DateTimeField('创建时间', auto_now_add=True)
modifyed_time = models.DateTimeField('修改时间', auto_now=True) def __str__(self):
return self.title class Meta:
ordering = ['-created_time']
verbose_name = ' 书籍正文'
verbose_name_plural = verbose_name

6.  设置数据库链接方式在settings.py中修改

import pymysql

pymysql.install_as_MySQLdb()

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'books', # 数据库名字
'USER': 'root', # 账号
'PASSWORD': '', # 密码
'HOST': 'localhost', # IP
'PORT': '', # 端口
}
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
}

7. 将新的模块book加入系统中

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'book',
]

8.  迁移数据、到数据库

╰$ python3 manage.py makemigrations
╰$ python3 manage.py migrate

9. 创建超级管理员,根据提示一步一步来

╰$ python3 manage.py createsuperuser

10. 在urls.py中进行路由配置

from django.conf.urls import url, include
from django.contrib import admin urlpatterns = [
url(r'admin/', admin.site.urls),
url(r'^', include('book.urls')),
]

11. 在book模块中,创建urls.py配置路由

from django.conf.urls import url
from . import views app_name = 'book'
urlpatterns = [
url(r'^$', views.index, name='book_index'),
url(r'^(?P<blog_id>[0-9]+)', views.detail, name='book_detail'),
]

12. 配置前端页面在Cornucopia/book下创建templates/book,两个目录,在settings.py配置templates路径

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

13. 创建index.html和detail.html

基于django快速开发一个网站(一)

14.配置静态文件目录在Cornucopia/book下创建static/css,两个目录,在settings.py配置templates路径

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static').replace('\\', '/'),
]

15.admin.py配置后台现在内容

from django.contrib import admin
from . import models class EntryAdmin(admin.ModelAdmin):
list_display = ['title','author','visiting','created_time','modifyed_time'] admin.site.register(models.Category)
admin.site.register(models.Tag)
admin.site.register(models.Entry,EntryAdmin)

16.启动程序、登入后台

╰$ python3 manage.py runserver 127.0.0.1:8001

基于django快速开发一个网站(一)

上一篇:(spring-第19回【AOP基础篇】)基于AspectJ和Schema的AOP


下一篇:MVC导入命名空间