Django admin page error

2019-04-02 11:18发布

问题:

I was creating a Django app. Doing so i enabled the Django admin site and i could see it working fine. Then i created some models and inserted data into it through the form in my app. But i am not able to see the entered values through admin panel of Django. I found that its because i havent included an admin.py file in my application folder. Then i created an admin.py file, ran syndb and tried. Now its throwing an error

ImportError at /admin/
No module named UniversityDetails.models

I will paste my admin.py, models.py and project folder structure. Please help me to solve this.

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)

Project Name :universityDB
Application Name : 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

回答1:

try

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)


回答2:

It sounds like UniversityDetails isn't in your python path, have you added it yet?



回答3:

The typical django setup puts the project's parent directory on the Python Path, meaning your models are resolved via MyProject.MyApp.models

If you can import universityDB (you would need to or you'd have more / different problems), you should change line 1 of your admin.py to from universityDB.universityDetails.models import * and you should be set.



回答4:

Use the following :

from models import *

and put your admin.py file in your model folder



回答5:

try turning managed = false in the model name (delete the managed = false in meta of model class)