Django管理页面错误

时间:2011-03-29 04:35:29

标签: python django django-models django-admin django-templates

我正在创建一个Django应用。这样做我启用了Django管理站点,我可以看到它正常工作。然后我创建了一些模型,并通过我的应用程序中的表单将数据插入其中。但是我无法通过Django的管理面板看到输入的值。我发现它是因为我没有在我的应用程序文件夹中包含一个admin.py文件。然后我创建了一个admin.py文件,运行syndb并尝试了。现在它抛出一个错误

ImportError at /admin/
No module named UniversityDetails.models

我将粘贴我的admin.py,models.py和项目文件夹结构。请帮我解决这个问题。

Models.py

from django.db import models

class UniversityDetails(models.Model):
    firstName = models.CharField(max_length=200)
    lastName = models.CharField(max_length=200)
    email = models.EmailField()
    password = models.TextField()
    birthday = models.TextField()
    sex = models.CharField(max_length=10)

admin.py

from universityDetails.models import *
from django.contrib import admin

admin.site.register(UniversityDetails)

项目名称: universityDB
申请名称: universityDetails

TRACEBACK

    Environment:

Request Method: GET
Request URL: http://10.1.0.90:8080/admin/
Django Version: 1.2.5
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'universityDetails',
 'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.csrf.CsrfResponseMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:

    File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
      91.                         request.path_info)
    File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in resolve
      215.             for pattern in self.url_patterns:
    File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
      244.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
      239.             self._urlconf_module = import_module(self.urlconf_name)
    File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py" in import_module
      35.     __import__(name)
    File "/home/rv/Desktop/universityDB/../universityDB/urls.py" in <module>
      5. admin.autodiscover()
    File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/__init__.py" in autodiscover
      24.             import_module('%s.admin' % app)
    File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py" in import_module
      35.     __import__(name)
    File "/home/rv/Desktop/universityDB/universityDetails/admin.py" in <module>
      1. from UniversityDetails.models import *

    Exception Type: ImportError at /admin/
    Exception Value: No module named UniversityDetails.models

5 个答案:

答案 0 :(得分:3)

from universityDB.universityDetails.models import *
from django.contrib import admin

class UniversityDetailsAdmin(admin.ModelAdmin):
    fields = ['firstName ', 'lastName ']
    list_display = ('firstName ', 'lastName ')    
    search_fields = ['firstName ', 'lastName ']

admin.site.register(UniversityDetails,UniversityDetailsAdmin)

答案 1 :(得分:1)

听起来像UniversityDetails不在你的python路径中,你添加它了吗?

答案 2 :(得分:1)

典型的django设置将项目的父目录放在Python Path上,这意味着您的模型通过MyProject.MyApp.models解析

如果你可以导入universityDB(你需要或者你有更多/不同的问题),你应该将admin.py的第1行更改为from universityDB.universityDetails.models import *,你应该是集。

答案 3 :(得分:0)

使用以下内容:

来自模型导入*

并将您的admin.py文件放在模型文件夹中

答案 4 :(得分:0)

尝试在模型名称中转换managed = false(删除模型类元中的managed = false)