参考官网文档,创建投票系统。
================
Windows 7/10
Python 2.7.10
Django 1.8.2
================
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 importmodels#Create your models here.#问题
classQuestion(models.Model):
question_text= models.CharField(max_length=200)
pub_date= models.DateTimeField('date published')def __unicode__(self):returnself.question_text#选择
classChoice(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
Migrationsfor 'polls':
0001_ini