Python Django 导入错误

0 投票
2 回答
11922 浏览
提问于 2025-04-17 00:26

我刚接触Django,正在用Python 2.6和Django 1.3版本来创建一个简单的博客应用。当我访问 http://127.0.0.1:8000/admin/ 时,出现了一个错误,提示 No module named urls。我在urls.py文件里写了这行代码: url(r'^admin/', include('django.contrib.admin.urls'))

urls.py

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

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # 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('django.contrib.admin.urls')),
)

settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mysite.blog',
    #Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentatio    n:  
    'django.contrib.admindocs',
 )

models.py

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

   class BlogPost(models.Model):
     title=models.CharField(max_length=150)
     body=models.TextField()
     timestamp=models.DateTimeField()

   admin.site.register(BlogPost)

错误信息:

ImportError at /admin/

No module named urls

Request Method:     GET
Request URL:    http://127.0.0.1:8000/admin/
Django Version:     1.3
Exception Type:     ImportError
Exception Value:    

No module named urls

Exception Location:     /usr/local/lib/python2.6/dist-packages/django/utils/importlib.py in import_module, line 35
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/bharathi/development/python/mysite',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/pymodules/python2.6/gtk-2.0']                                       

有没有什么建议?

2 个回答

0

你有没有在你的settings.py文件中的INSTALLED_APPS列表里启用django.contrib.admin?

Django只有在你把Python模块(在Django里也叫“Django应用”)写在settings.py文件的INSTALLED_APPS列表里时,才会知道它们的存在。

1

谢谢你。之前的代码缩进不正确。我已经修好了。

撰写回答