记录一个产品的开始:基于django快速开发一个网站(一)
创建虚拟环境、基于虚拟环境创建django==2.0.0和图片加载库和mysql数据库驱动
1
创建目录并创建虚拟环境
1
╰$ mkdir Cornucopiavirtualenv ╰$ cd Cornucopiavirtualenv ╰$ virtualenv env1 123
进入bin目录,并激活虚拟环境
╰$ cd bin ╰$ source activate12
进入环境、创建django和Pillow、PyMySQL
╰$ pip install django==2.0.0 ╰$ pip install pillow ╰$ pip install pymysql 或者也可以 ╰$ pip install pymysql pillow django==2.0.012345
创建Cornucopia的项目并设置book模块,并设置中文和中国上海的时区
╰$ django-admin startproject Cornucopia ╰$ python3 manage.py startapp book12
4-1. 设置中文和中国上海的时区(在settings.py中修改)
LANGUAGE_CODE = 'zh-hans'TIME_ZONE = 'Asia/Shanghai'12
创建model模型数据
from django.db import modelsfrom django.contrib.auth.models import Userfrom mdeditor.fields import MDTextField# Create your models here.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.nicknameclass Category(models.Model): name = models.CharField('分类名称', max_length=128) def str(self): return self.name class Meta: verbose_name = '书籍分类' verbose_name_plural = verbose_nameclass Tag(models.Model): name = models.CharField('标签', max_length=128) def str(self): return self.name class Meta: verbose_name = '书籍标签' verbose_name_plural = verbose_nameclass 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_name12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
设置数据库链接方式在settings.py中修改
import pymysqlpymysql.install_as_MySQLdb()DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'books', # 数据库名字 'USER': 'root', # 账号 'PASSWORD': '', # 密码 'HOST': 'localhost', # IP 'PORT': '3306', # 端口 } # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # }}123456789101112131415161718
将新的模块book加入系统中
# Application definitionINSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'book',]1234567891011
迁移数据、到数据库
╰$ python3 manage.py makemigrations ╰$ python3 manage.py migrate12
创建超级管理员,根据提示一步一步来
╰$ python3 manage.py createsuperuser1
在urls.py中进行路由配置
from django.conf.urls import url, includefrom django.contrib import adminurlpatterns = [ url(r'admin/', admin.site.urls), url(r'^', include('book.urls')),]1234567
11.在book模块中,创建urls.py配置路由
from django.conf.urls import urlfrom . import viewsapp_name = 'book'urlpatterns = [ url(r'^$', views.index, name='book_index'), url(r'^(?P<blog_id>[0-9]+)', views.detail, name='book_detail'),]12345678
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', ], }, },]123456789101112131415
13.创建index.html和detail.html
14.配置静态文件目录在Cornucopia/book下创建static/css,两个目录,在settings.py配置templates路径
STATIC_URL = '/static/'STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static').replace('\', '/'),]1234
15.admin.py配置后台现在内容
from django.contrib import adminfrom . import modelsclass 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)123456789
16.启动程序、登入后台
╰$ python3 manage.py runserver 127.0.0.1:80011
赞 (0)