简体   繁体   English

Django管理界面不显示应用程序

[英]Django Admin Interface doesn't Show App

I'm trying to follow this To-Do Application tutorial (link) with some tiny differences on my system 我正在尝试按照这个To-Do应用程序教程(链接)来解决我的系统上的一些细微差别

  • I use sqLite instead of mysql I use django 1.4 . 我使用sqLite而不是mysql我使用django 1.4。
  • I think tutorial was written before 1.4 released. 我认为教程是在1.4发布之前编写的。

I have only one app as it's named in tutorial- todo. 我只有一个应用程序,因为它在tutorial-todo中命名。 I'm trying to display app on django's admin interface but i can't manage to do that. 我正试图在django的管理界面上显示应用程序,但我无法做到这一点。

When i type python manage.py syncdb command on terminal, It gives me this error message: 当我在终端上键入python manage.py syncdb命令时,它给出了以下错误消息:

终端中的错误消息

Below you can see my project's files. 您可以在下面看到我的项目文件。


models.py 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 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 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 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)

The problem is that you're running admin.autodiscover() from within your models module, so the following happens during that call: 问题是您从models模块中运行admin.autodiscover() ,因此在该调用期间会发生以下情况:

  1. Django looks for all admin modules in all installed apps, imports them Django在所有已安装的应用程序中查找所有admin模块,然后导入它们
  2. Imports at the top of admin modules are of course run right when each admin is imported 当然,每个admin导入时, admin模块顶部的导入都会正确运行
  3. The admin module imports from todo.models import List (presumably) admin模块from todo.models import List (推测)
  4. The todo.models module isn't available yet, because it was still being imported by load_app (6th frame from the bottom of your traceback) when admin.autodiscover() was run (3rd frame from the bottom of your traceback) todo.models模块尚不可用,因为当运行admin.autodiscover()时, admin.autodiscover()从跟踪的底部第6帧)仍然会导入load_app (从跟踪底部开始的第3帧)

tl;dr TL;博士

You just have a circular import, but I wanted to explain it so that it was clear why. 你只是有一个循环导入,但我想解释它,以便明确原因。

Solution

Move admin.autodiscover() to your main urls module. admin.autodiscover()移动到主urls模块。

It's caused for circular import issue: 它导致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
|           |
|           |
+-----------+

Your admin style is old, try new admin style. 你的admin风格很旧,尝试新的管理风格。

from django.utils.encoding import smart_text 
def _str_(self) :
        return smart_text(self.title)

for title not displaying in django admin panel 标题不在django管理面板中显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM