1.新建HelloWorld项目
django-admin.py startproject HelloWorld
2.新建一个app--blog
# 进入 HelloWorld 文件夹
cd HelloWorld
# 创建 blog 这个 app
python manage.py startapp blog
3.修改blog文件中的models.py
<span style="font-size:10px;"># coding:utf-8
from django.db import models
class Article(models.Model):
title = models.CharField(u'标题', max_length=256)
content = models.TextField(u'内容')
pub_date = models.DateTimeField(u'发表时间', auto_now_add=True, editable = True)
update_time = models.DateTimeField(u'更新时间',auto_now=True, null=True)</span>
<span style="font-size:10px;"># coding:utf-8
from django.db import models
class Article(models.Model):
title = models.CharField(u'标题', max_length=256)
content = models.TextField(u'内容')
pub_date = models.DateTimeField(u'发表时间', auto_now_add=True, editable = True)
update_time = models.DateTimeField(u'更新时间',auto_now=True, null=True)</span>
4.blog 加入到settings.py中的INSTALLED_APPS中
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'blog'
,
)
5.同步所有数据表
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'blog'
,
)
<span style="font-size:10px;"># 进入包含有 manage.py 的文件夹
python manage.py makemigrations
python manage.py migrate
注意:Django 1.6.x 及以下的版本需要用以下命令
python manage.py syncdb</span>
<span style="font-size:10px;"># 进入包含有 manage.py 的文件夹
python manage.py makemigrations
python manage.py migrate
注意:Django 1.6.x 及以下的版本需要用以下命令
python manage.py syncdb</span>
6.修改 admin.py
from django.contrib import admin
from .models import Article
admin.site.register(Article)
7.打开开发服务器
python manage.py runserver
# 如果提示 8000 端口已经被占用,可以用 python manage.py runserver 8001 以此类推
访问 http://localhost:8000/admin/ 输入设定的帐号和密码, 就可以看到:
点击 Articles,动手输入 添加几篇文章
我们会发现所有的文章都是叫 Article object,这样不好,修改一下 blog 中的models.py
# coding:utf-8
from django.db import models
class Article(models.Model):
title = models.CharField(u'标题', max_length=256)
content = models.TextField(u'内容')
pub_date = models.DateTimeField(u'发表时间', auto_now_add=True, editable = True)
update_time = models.DateTimeField(u'更新时间',auto_now=True, null=True)
def __unicode__(self):# 在Python3中用 __str__ 代替 __unicode__
return self.title
8.在列表显示与字段相关的其它内容
<span style="font-size:10px;">from django.contrib import admin
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title','pub_date','update_time',)
admin.site.register(Article,ArticleAdmin)</span>
list_display 就是来配置要显示的字段的,当然也可以显示非字段内容,或者字段相关的内容,比如:
<span style="font-size:10px;">class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
def my_property(self):
return self.first_name + ' ' + self.last_name
my_property.short_description = "Full name of the person"
full_name = property(my_property)</span>
在admin.py中
<span style="font-size:10px;">from django.contrib import admin
from .models import Article, Person
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'pub_date', 'update_time',)
class PersonAdmin(admin.ModelAdmin):
list_display = ('full_name',)
admin.site.register(Article, ArticleAdmin)
admin.site.register(Person, PersonAdmin)</span>