python - 添加按钮未显示在 Django 教程 02 中

标签 python django admin addition

我是 Django 新手,一直在阅读 www.djangoproject.com 上的官方教程。我已在我的系统中成功实现了教程 1,但我无法弄清楚为什么管理面板中没有显示“加号”或“添加”按钮

我正在使用django 1.6.1 这是一个非常简单的代码,但我无法弄清楚,因为我对 Django 没有任何先验知识。我们将不胜感激。

下面是文件 models.py 和 admin.py 的代码

模型.py

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):  # Python 3: def __str__(self):
        return self.question

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __unicode__(self):  # Python 3: def __str__(self):
         return self.choice_text

admin.py

from django.contrib import admin
from polls.models import Choice, Poll

"""class ChoiceInline(admin.TabularInline):
    model = Choice

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    list_display = ('question', 'pub_date')
    inlines = [ChoiceInline]
    list_filter = ['pub_date']
    search_fields = ['question']
"""
admin.site.register(Choice)

最佳答案

首先,不清楚为什么admin.py中的类要用三引号写在字符串内。

假设这是一个拼写错误/故意的,您仍然需要 register() PollAdmin:

admin.site.register(Poll, PollAdmin)
<小时/>

此步骤的完整代码应如下所示:

from django.contrib import admin
from polls.models import Choice, Poll

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]

admin.site.register(Choice)
admin.site.register(Poll, PollAdmin)

关于python - 添加按钮未显示在 Django 教程 02 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701147/

相关文章:

python - 编辑列表,同时将其与另一个列表作为约束进行比较

python - 客户端排序查询集,Django + TwitterBoostrap

python - Django allauth 中的动态 login_redirect_url

asp.net - 使用管理面板编辑网站的 DotNetNuke 皮肤 CSS

Php 管理面板在页脚下方显示编辑帖子表单

Python MySQLdb : table does not get deleted immediately

python - (TypeError : expected string or bytes-like object) when calling function in Django

python - 使用 Google App Engine 的 Channel API 和开发服务器时出现 JavaScript 错误

python - django 教程第 4 部分中定义的变量 error_message 在哪里

python - 如何使用测试客户端填写 Django 表单