• Django 应用开发(2)


    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文件。

     结果:

  • 相关阅读:
    吾爱破解2018-2020优秀文章合集
    分享一个零基础入门学习Python(第2版)带课件及源码
    fiddler抓包工具详细配置方法,多图详细(转)
    浏览器提速,支持95%的国产浏览器(转)
    易语言5.92学习版
    Android Studio 之 ViewModel (转)
    一款易语言软件启动前修改(劫持DLL注入修改)【转】
    某桌球辅助登录算法分析并转本地验证
    软件虚拟机保护分析资料整理
    HttpCanary其他教程
  • 原文地址:https://www.cnblogs.com/fjl-vxee/p/6803983.html
Copyright © 2020-2023  润新知