1

我正在通过http://lightbird.net/dbe/todo_list.html上的 Django 教程工作。我完成了 Django 的官方教程。当我尝试同步时

from django.db import models
from django.contrib import admin

class Item(models.Model):
    name = models.CharField(max_length=60)
    created = models.DateTimeField(auto_now_add=True)
    priority = models.IntegerField(default=0)
    difficult = models.IntegerField(default=0)

class ItemAdmin(admin.ModelAdmin):
    list_display = ["name", "priority", "difficult", "created", "done"]
    search_fields = ["name"]

admin.site.register(Item, ItemAdmin)

终端返回一个错误,说管理员未定义。我究竟做错了什么?我如何定义管理员?

更新:我添加了现在看起来的整个模型文件

4

1 回答 1

0

The Django documentation lists multiple steps that need to be done to activate the admin site:

  1. Add 'django.contrib.admin' to your INSTALLED_APPS setting.

  2. The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and django.contrib.sessions. If these applications are not in your INSTALLED_APPS list, add them.

  3. Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to MIDDLEWARE_CLASSES. (These are both active by default, so you only need to do this if you’ve manually tweaked the settings.)

  4. Determine which of your application’s models should be editable in the admin interface.

  5. For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for that particular model.

  6. Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.

  7. Hook the AdminSite instance into your URLconf. lists a couple of things that you must do the enable Django's admin site.

If you haven't done the first two items, syncdb might complain because it can't find the admin app itself.

于 2012-09-28T16:33:29.670 回答