标签:
入门笔记翻译整理自:https://docs.djangoproject.com/en/1.8/
*该笔记将使用一个关于投票网络应用(poll application)的例子来阐述Django的用法。
运行代码
1 $ python manage.py createsuperuser 2 3 Username: admin 4 Email address: admin@example.com 5 6 Password: ********* 7 Password(again): ********* 8 Superuser created successfully.
启动服务器
1 $ python manage.py runserver
打开本地域名,比如http://127.0.0.1:8000/admin/,浏览器将显示管理员登陆页面。
输入用户名和密码
投票应用显示在管理员页面
1 #polls/admin.py 2 3 from django.contrib import admin 4 5 from .models import Question 6 7 admin.site.register(Question)
略
对admin.site.register(Question)进行替换
1 #polls/admin.py 2 3 from django.contrib import admin 4 5 from .models import Question 6 7 class QuestionAdmin(admin.ModelAdmin): 8 fields = [‘pub_date‘, ‘question_text‘] 9 10 admin.site.register(Question, QuestionAdmin)
基本的思路是:创建一个ModelAdmin对象,然后将它作为第二个参数传递到register()方法中
再比如,
1 #polls/admin.py 2 3 from django.contrib import admin 4 5 from .models import Question 6 7 class QuestionAdmin(admin.ModelAdmin): 8 fieldsets = [ 9 (None, {‘fields‘: [‘question_text‘]}), 10 (‘Date information‘, {‘fields‘: [‘pub_date‘], ‘classes‘:[‘collapse‘]}), 11 ] 12 13 admin.site.register(Question, QuestionAdmin)
在投票应用中,Question有关联对象Choice。有两种方式添加关联对象
1 #polls/admin.py 2 3 from django.contrib import admin 4 5 from .models import Choice, Question 6 # ... 7 admin.site.register(Choice)
还可以
1 #polls/admin.py 2 3 from django.contrib import admin 4 5 from .models import Choice, Question 6 7 class ChoiceInline(admin.StackedInline): 8 model = Choice 9 extra = 3 10 11 class QuestionAdmin(admin.ModelAdmin): 12 fieldsets = [ 13 (None, {‘fields‘: [‘question_text‘]}), 14 (‘Date information‘,{‘fields‘: [‘pub_date‘], ‘classes‘: [‘collapse‘]}), 15 ] 16 inlines = [ChoiceInline] 17 18 admin.site.register(Question, QuestionAdmin)
此外,以下代码可以让界面简洁
1 # polls/admin.py 2 3 class ChoiceInline(admin.TabularInline): 4 # ...
运行代码
1 # polls/admin.py 2 3 class QuestionAdmin(admin.ModelAdmin): 4 # ... 5 list_display = (‘question_text‘, ‘pub_date‘, ‘was_published_recently‘)
对于自定义方法was_published_recently,排序在默认状态下不能实现,且列表名称为方法名称,可以通过以下方式改进
1 # polls/models.py 2 3 class Question(models.Model): 4 # ... 5 def was_published_recently(self): 6 return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 7 was_published_recently.admin_order_field = ‘pub_date‘ 8 was_published_recently.boolean = True 9 was_published_recently.short_description = ‘Published recently?‘
可以向QuestionAdmin对象添加过滤器和搜索功能
1 # polls/admin.py 2 3 class QuestionAdmin(admin.ModelAdmin): 4 # ... 5 list_filter = [‘pub_date‘] 6 search_fields = [‘question_text‘]
在manage.py所在目录下,创建templates子目录。添加DIRS到设置文件(mysite/settings.py,牢记)中的TEMPLATES设置:
1 TEMPLATES = [ 2 { 3 ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, 4 ‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)], 5 ‘APP_DIRS‘: True, 6 ‘OPTIONS‘: { 7 ‘context_processors‘: [ 8 ‘django.template.context_processors.debug‘, 9 ‘django.template.context_processors.request‘, 10 ‘django.contrib.auth.context_processors.auth‘, 11 ‘django.contrib.messages.context_processors.messages‘, 12 ], 13 }, 14 }, 15 ]
在templates目录中,建立admin子目录,并复制django/contrib/admin/templates中的模板admin/base_site.html。然后对base_site.html进行修改。
步骤和9类似
--The End--
标签:
原文地址:http://www.cnblogs.com/py-drama/p/4579983.html