bae3.0第四步 第一个polls系统

1、创建自己的app
进入新建的blog工程目录,执行其下面的manage.py来创建polls应用,命令为:
python manage.py startapp polls
2、定义app的models
Each model is represented by a class that subclasses django.db.models.Model.
Each model has a number of class variables, each of which represents a database field in the model.
for example:
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
3、安装app
Edit the settings.py file, and change the INSTALLED_APPS setting to include the string 'polls'.
4、创建app需要的表
4.1、生成app需要的表创建sql执行命令: python manage.py sql polls
4.2、在数据库中执行上面生成的sql语句。
5、add __unicode__() methods to your models
We use __unicode__() here because Django models deal with Unicode by default.
All data stored in your database is converted to Unicode when it's returned.
Django models have a default __str__() method that calls __unicode__()
and converts the result to a UTF-8 bytestring.
This means that unicode(p) will return a Unicode string,
and str(p) will return a normal string, with characters encoded as UTF-8.
6、Make the poll app modifiable in the admin
Open polls/admin.py,and edit it to look like this:
from polls.models import Poll
admin.site.register(Poll)
如果想自己定义管理界面,那么自定义一个类,并重新注册,如:
class PollAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question']
admin.site.register(Poll, PollAdmin)
7、Customizing your project’s templates
by default, Django automatically looks for a templates/ subdirectory within each application package
8、修改工程的urls.py
添加内容: url(r'^polls/', include('polls.urls', namespace="polls")),
9、中间用到的文件可以参考django的官方网站https://docs.djangoproject.com/en/1.6/intro/tutorial03/

原文地址:https://www.cnblogs.com/zxpo/p/3532613.html