参考官网文档,创建投票系统。
================
Windows 7/10
Python 3.6
Django 2.0*
================
1、创建项目(mysite)与应用(polls)
D:\pydj>django-admin.py startproject mysite
D:\pydj>cd mysite
D:\pydj\mysite>python manage.py startapp polls
添加到setting.py
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
最终哪个目录结构:
2、创建模型(即数据库)
一般web开发先设计数据库,数据库设计好了,项目就完了大半了,可见数据库的重要性。打开polls/models.py编写如下:
# coding=utf-8
from django.db import models
# Create your models here.
# 问题
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question_text
# 选择
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
执行数据库表生成与同步。
D:\pydj\mysite>python manage.py makemigrations polls
Migrations for 'polls':
0001_initial.py:
- Create model Qu