【问题标题】:Django Admin Interface doesn't Show AppDjango 管理界面不显示应用程序
【发布时间】:2014-03-23 00:56:17
【问题描述】:

我正在尝试关注这个To-Do Application tutorial (link),但我的系统上有一些细微差别

  • 我使用 sqLite 而不是 mysql 我使用 django 1.4。
  • 我认为教程是在 1.4 发布之前编写的。

我只有一个应用程序,它在教程中命名 - 待办事项。我正在尝试在 django 的管理界面上显示应用程序,但我无法做到。

当我在终端上输入 python manage.py syncdb 命令时,它给了我这个错误信息:

您可以在下面看到我的项目文件。


models.py

# -*- coding: utf-8 -*-


from django.db import models
from django.contrib import admin
admin.autodiscover()

# Create your models here.

# For this application, we will need two models:
#                                              one representing a list,
#                                              and one representing an item in a list.


# this class will be a database table named list
class List(models.Model): 

  title = models.CharField(max_length=250, unique=True) 

  # __str__ method is like toString() in java
  def __str__(self): 

    return self.title 

  class Meta: 

    ordering = ['title'] 

  class Admin: 

    pass




# i need this for  Item Class    
import datetime 



PRIORITY_CHOICES = ( 

  (1, 'Low'), 

  (2, 'Normal'), 

  (3, 'High'), 

) 


# this class will be a database table named item
class Item(models.Model): 

  # this will create a charfield column named "title" in database
  title = models.CharField(max_length=250) 


  # created_date will be a DATETIME column in the database
  # datetime.datetime.now is a standard Python function
  created_date = models.DateTimeField(default=datetime.datetime.now) 


  # default priority level will be 2 as in "Normal" in PRIORITY_CHOICES
  # using choices argument as choices=PRIORITY_CHOICES , Django will allow only 1,2 and 3 as we want to
  priority = models.IntegerField(choices=PRIORITY_CHOICES, default=2) 


  # this will create a boolean column named "completed" in database
  completed = models.BooleanField(default=False)

  todo_list = models.ForeignKey(List) 

  def __str__(self): 

    return self.title 

  class Meta: 

    # We have specified that list items should be ordered by two columns: priority and title.
    # The - in front of priority tells Django to use descending order for the priority column,
    # so Django will include ORDER BY priority DESC title ASC in its queries whenever it deals with list items.
    ordering = ['-priority', 'title'] 

  class Admin: 

    pass

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/Users/ihtechnology/Desktop/envi/db_gtd/sqlite3.db', 
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      
        'PORT': '',                      
    }
}

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    # I ADDED MY APPLICATION HERE SO IN CAN BE INSTALLED INTO PROJECT AS OTHER DEFAULT DJANGO APPS
    'todo',
)

urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

admin.py

from todo.models import List
from todo.models import Item
from django.contrib import admin

admin.site.register(List)
admin.site.register(Item)

【问题讨论】:

    标签: python django django-admin


    【解决方案1】:

    问题是您在 models 模块中运行 admin.autodiscover(),因此在该调用期间会发生以下情况:

    1. Django 查找所有已安装应用程序中的所有 admin 模块,并导入它们
    2. admin 模块顶部的导入当然会在每个 admin 被导入时正确运行
    3. admin 模块导入from todo.models import List(推测)
    4. todo.models 模块尚不可用,因为当 admin.autodiscover() 运行时(从您的回溯底部算起的第 3 帧),它仍在由 load_app(从您的回溯底部算起的第 6 帧)导入

    tl;博士

    你只是有一个循环导入,但我想解释一下,以便清楚为什么。

    解决方案

    admin.autodiscover() 移动到您的主 urls 模块。

    【讨论】:

      【解决方案2】:

      这是circular import问题引起的:

      +--> +/todo/models.py #line 6
      |    |
      |    +->**admin.autodiscover()**
      |      +
      |      |
      |      +-->/django/contrib/admin/__init__.py #line 29
      |         +
      |         |
      |         +->/django/utils/importlib.py # line 35
      |           +
      |           |
      |           +-->/todo/admin.py #line 1
      |           |
      |           +->from todo.models import List
      |           |
      |           |
      +-----------+
      

      您的admin 样式已旧,请尝试新的admin 样式。

      【讨论】:

      • 我使用的是 django 1.4,新的管理风格是从 1.6 开始的 我解决了这个问题,但感谢你的帮助
      【解决方案3】:
      from django.utils.encoding import smart_text 
      def _str_(self) :
              return smart_text(self.title)
      

      标题未显示在 django 管理面板中

      【讨论】:

      • 我不确定这是否适用于这个问题;查看根本原因的其他答案,这似乎根本没有解决。
      猜你喜欢
      • 2010-12-14
      • 1970-01-01
      • 2011-09-01
      • 2014-04-26
      • 2017-08-12
      • 2011-01-27
      • 1970-01-01
      • 2013-07-09
      • 2013-03-19
      相关资源
      最近更新 更多