无法在django中导入模式

时间:2014-03-20 12:16:19

标签: python django

我在django url.py文件中定义url。每当我把网址放在浏览器中并运行它说:

cannot import name patterns
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/
Django Version: 1.2.4
Exception Type: ImportError
Exception Value:    
cannot import name patterns
Exception Location: C:\Python27\lib\site-packages\registration\backends\default\urls.py in <module>, line 20
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.1
Python Path:    ['C:\\Users\\Tameen\\lmn', 'C:\\Python27\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\PIL']
Server time:    Thu, 20 Mar 2014 17:17:58 +0500

我的Url.py文件是:

    from django.conf.urls.defaults import *

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

urlpatterns = patterns('',

    (r'^accounts/', include('registration.backends.default.urls')),
    (r'^admin/', include(admin.site.urls)),
    (r'^pastebin/', include('pastebin.urls')),
    (r'^blog/', include('blog.urls')),
    # Example:
     (r'^lmn/', include('lmn.foo.urls')),

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

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

3 个答案:

答案 0 :(得分:2)

您正在使用最新版本的django-registration(1.0)和{非常旧版本的django(1.2)。 django-registration在其urls.py中使用the following code

from django.conf.urls import patterns

使用您的django版本,需要

from django.conf.urls.defaults import patterns

您有两种选择:

  1. Upgrade your version of Django。如果您使用pip,则可以执行:pip install django==1.6
  2. 降级django-registration的版本。如果您使用pip,则可以执行pip install django-registration==0.8(或者可能为0.7)
  3. 显然第一个选项是首选,因为1.2是一个非常古老的django版本

答案 1 :(得分:1)

您的错误不在urls.py中,而在registration应用程序模块中,可能与Django 1.2.X兼容。

最后一个版本是1.6.2

答案 2 :(得分:0)

Django新版本(1.8及以上版本)与

不兼容
from django.conf.urls import patterns

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),)

结构。使用它代替它。在你的myapp目录中&gt; views.py包含您想要获得的视图..例如:

from __future__ import unicode_literals
from django.http import HttpResponse

def hello(request):
    text = """<h1>welcome to my app !</h1>"""
    return HttpResponse(text)

然后在你的urls.py中包含以下代码。

from django.conf.urls import   url , include
from django.contrib import admin
from myapp.views import hello
admin.autodiscover()


urlpatterns =[

    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/',hello),
]

然后你应该能够正确渲染你好的视图。 :)