Django升级到1.9

时间:2015-12-23 21:25:00

标签: django django-urls

我将我的Django版本演变为1.9(在我拥有1.6或1.7之前),所以我修改了很多obseltes的东西......

但我的urls.py中的这些行有问题:

import django
import main_app 
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView, ListView
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from main_app.views import *
from main_app.views import password_reset_confirm
... # many urls
url(r'^authentification/$', django.contrib.auth.views.login),
url(r'^forget/send/$', django.contrib.auth.views.password_reset_done),
url(r'^password/$', django.contrib.auth.views.password_reset),
url(r'^password_forget/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', main_app.views.password_reset_confirm),
url(r'^password-init/$', django.contrib.auth.views.password_reset_complete),

当我编写“python manage.py runserver”时出现此错误:

Unhandled exception in thread started by <function wrapper at 0x7f5bcf01af50>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in  import_module
__import__(name)
File "/home/yb/web/carzuip/carzuip/urls.py", line 55, in <module>
url(r'^authentification/$', django.contrib.auth.views.login),
NameError: name 'django' is not defined

我不明白为什么我只有这5个网址有问题?!

感谢的

3 个答案:

答案 0 :(得分:4)

该代码没有显示您导入django:它会显示您在其下方导入元素,但从不会导致名称本身。 Python的基本原则是您必须导入或定义您使用的每个名称。在您的情况下,顶部的import django会起作用,但请注意,当代码到达password_reset网址时您会遇到另一个问题,因为它是从main_app引用的,您再次无法导入

答案 1 :(得分:2)

尝试使用django-admin startproject创建新项目,并通过manage.py和wsgi.py复制所需的设置/副本。

某些版本发生了重大变化,即wsgi模块如何导入某些django内容。大多数项目都可以轻松重新创建,并且文件中有最新的django默认值。

这当然只是猜测,但值得一试。如果你正在使用virtualenv,你可能需要重新创建一些内容,当你的python升级/你移动了virtualenv。这样的东西有时会很烦人。

答案 2 :(得分:2)

已解决!

Euh我想...

我添加以下几行:

from django.contrib.auth.views import login 
from django.contrib import auth

它的作品!

这是正常的hein?