Django 应用开发
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 应用开发相关的知识,希望对你有一定的参考价值。
1.创建一个管理员用户
创建一个用于登录管理站点的用户
2.启动开发服务器
python manage.py runserver
打开浏览器访问本地域名中的/admin/
3.让poll应用在管理站点中可编辑
在polls/admin.py中编辑
4.修改界面
在polls/admin.py中编辑
1 from django.contrib import admin 2 from .models import Choice,Question 3 4 class ChoiceInline(admin.TabularInline): 5 model = Choice 6 extra = 3 7 8 class QuestionAdmin(admin.ModelAdmin): 9 fieldsets = [ 10 (None,{‘fields‘:[‘question‘]}), 11 (‘Date information‘,{‘fields‘:[‘pub_date‘],‘classes‘:[‘collapse‘]}) 12 ] 13 inlines = [ChoiceInline] 14 admin.site.register(Question,QuestionAdmin) 15 # Register your models here.
自定义管理界面中的变更列表
admin.py
1 from django.contrib import admin 2 from .models import Choice,Question 3 4 class ChoiceInline(admin.TabularInline): 5 model = Choice 6 extra = 3 7 8 class QuestionAdmin(admin.ModelAdmin): 9 list_display = (‘question‘,‘pub_date‘,‘was_published_recently‘) 10 fieldsets = [ 11 (None,{‘fields‘:[‘question‘]}), 12 (‘Date information‘,{‘fields‘:[‘pub_date‘],‘classes‘:[‘collapse‘]}) 13 ] 14 inlines = [ChoiceInline] 15 list_filter = [‘pub_date‘] 16 search_fields = [‘question‘] 17 18 admin.site.register(Question,QuestionAdmin) 19 # Register your models here.
models.py
1 from __future__ import unicode_literals 2 3 import datetime 4 from django.db import models 5 from django.utils import timezone 6 7 8 class Question(models.Model): 9 question = models.CharField(max_length=200) 10 pub_date = models.DateTimeField(‘date published‘) 11 12 def __str__(self): 13 return self.question 14 15 def was_published_recently(self): 16 return self.pub_date >=timezone.now() - datetime.timedelta(days=1) 17 18 was_published_recently.admin_order_field = ‘pub_date‘ 19 was_published_recently.boolean = True 20 was_published_recently.short_description = ‘Published recently?‘ 21 22 23 class Choice(models.Model): 24 poll = models.ForeignKey(Question) 25 choice_text = models.CharField(max_length=200) 26 votes = models.IntegerField(default=0) 27 28 def __str__(self): 29 return self.choice_text 30 31 # Create your models here.
界面:
自定义站点的外观
在templates中添加dirs选项
创建templates文件夹
在settings.py中添加。现在,在templates下创建一个名为admin的文件夹,然后从Django安装的原目录下(目录为django/contrib/admin/templates)将模板页面的源文件admin/base_site.html拷贝到这个文件夹里。并修改其中的html文件。
结果:
以上是关于Django 应用开发的主要内容,如果未能解决你的问题,请参考以下文章