美文网首页
08 - Customing the admin site

08 - Customing the admin site

作者: AureliusShu | 来源:发表于2018-09-12 22:04 被阅读0次

Customing the admin site

  1. Customize the admin form

    # polls/admin.py
    from django.contrib import admin
    from .models import Question
    
    class QuestionAdmin(admin.ModelAdmin):
        fields = ['pub_date', 'question_text']
    
    admin.site.register(Question, QuestionAdmin)
    
    # polls/admin.py
    from django.contrib import admin
    from .models import Question
    
    class QuestionAdmin(admin.ModelAdmin):
        fieldsets = [
            (None,               {'fields': ['question_text']}),
            ('Date information', {'fields': ['pub_date']}),
        ]
    
    admin.site.register(Question, QuestionAdmin)
    
  2. Adding related objects

    # polls/admin.py
    from django.contrib import admin
    from .models import Choice, Question
    # ...
    admin.site.register(Choice)
    
    # polls/admin.py
    from django.contrib import admin
    from .models import Choice, Question
    
    class ChoiceInline(admin.StackedInline):
        model = Choice
        extra = 3
    
    class QuestionAdmin(admin.ModelAdmin):
        fieldsets = [
            (None,               {'fields': ['question_text']}),
            ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
        ]
        inlines = [ChoiceInline]
    
    admin.site.register(Question, QuestionAdmin)
    
    # polls/admin.py
    class ChoiceInline(admin.TabularInline):
        #...
    
  3. Customize the admin change list

    polls/admin.py
    class QuestionAdmin(admin.ModelAdmin):
        # ...
        list_display = ('question_text', 'pub_date')
    
    # polls/admin.py
    class QuestionAdmin(admin.ModelAdmin):
        # ...
        list_display = ('question_text', 'pub_date', 'was_published_recently')
    
    # polls/models.py
    class Question(models.Model):
        # ...
        def was_published_recently(self):
            now = timezone.now()
            return now - datetime.timedelta(days=1) <= self.pub_date <= now
        was_published_recently.admin_order_field = 'pub_date'
        was_published_recently.boolean = True
        was_published_recently.short_description = 'Published recently?'
    
    list_filter = ['pub_date']
    
    search_fields = ['question_text']
    
  4. Customizing your project’s templates

    # mysite/settings.py
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    
    # Where are the Django source files?
    $ python -c "import django; print(django.__path__)"
    
    {% block branding %}
    <h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>
    {% endblock %}
    

相关文章

网友评论

      本文标题:08 - Customing the admin site

      本文链接:https://www.haomeiwen.com/subject/gqxigftx.html