问题描述
嗨,我无法解决此问题,
我遵循了教程。
我不记得了,但是我想我读过某个地方,如果您使用功能视图,则必须导入某些内容,但不确定此处是否是这种情况,或者这是否成立。
这是我的帐户应用程序中urls.py的代码
from django.conf.urls import patterns, url
from accounts import views
urlpatterns = patterns(
url(r'^register$', 'accounts.views.register', name='register'),
url(r'^login$', 'accounts.views.login', name='login'),
url(r'^logout$', 'accounts.views.logout', name='logout'),
)
在主要的urls.py中
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = ['',
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^admin/', include(admin.site.urls)),
]
下面是错误消息和回溯
Environment:
Django Version: 1.8.5
Python Version: 3.4.0
Request Method: GET
Request URL: http://127.0.0.1:8777/
Django Version: 1.8.5
Python Version: 3.4.0
Request Method: GET
Request URL: http://127.0.0.1:8777/
Django Version: 1.8.5
Exception Type: TypeError
Exception Value:
unsupported operand type(s) for +: 'RegexURLPattern' and 'str'
Exception Location: /home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/core/urlresolvers.py in add_prefix, line 223
Python Executable: /home/kevin/djangoP/envRU/bin/python
Python Version: 3.4.0
Traceback:
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
365. for pattern in self.url_patterns:
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/core/urlresolvers.py" in url_patterns
401. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/core/urlresolvers.py" in urlconf_module
395. self._urlconf_module = import_module(self.urlconf_name)
File "/home/kevin/djangoP/envRU/lib/python3.4/importlib/__init__.py" in import_module
109. return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kevin/djangoP/ru/rulinked/rulinked/urls.py" in <module>
20. url(r'^accounts/', include('accounts.urls', namespace='accounts')),
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/conf/urls/__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "/home/kevin/djangoP/envRU/lib/python3.4/importlib/__init__.py" in import_module
109. return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kevin/djangoP/ru/rulinked/accounts/urls.py" in <module>
7. url(r'^logout$', 'accounts.views.logout', name='logout'),
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/conf/urls/__init__.py" in patterns
61. t.add_prefix(prefix)
File "/home/kevin/djangoP/envRU/lib/python3.4/site-packages/django/core/urlresolvers.py" in add_prefix
223. self._callback_str = prefix + '.' + self._callback_str
Exception Type: TypeError at /
Exception Value: unsupported operand type(s) for +: 'RegexURLPattern' and 'str'
如果您读了那么远,那么谢谢,我知道很多,但我真的迷失了方向,不知道还能从哪里获得帮助。
1楼
您会混淆两种可用的语法。
patterns()
函数将前缀作为第一个参数,它是一个(可能为空)字符串,并返回一个列表,其中将前缀添加到每个模式中。
其他参数是url()
实例。
普通列表应仅包含url()
实例,并且不能添加公共前缀。
patterns()
已被弃用,因此最好在任何地方使用一个列表:
account / urls.py
urlpatterns = [
url(r'^register$', 'accounts.views.register', name='register'),
url(r'^login$', 'accounts.views.login', name='login'),
url(r'^logout$', 'accounts.views.logout', name='logout'),
]
urls.py
urlpatterns = [
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^admin/', include(admin.site.urls)),
]
至于导入视图:如果传递函数本身,则需要导入它们。
如果不先导入,则该功能不可用,并导致NameError
。
现在,您传递的是导入路径,而不是函数,因此您不必导入任何内容。
这也已弃用(用于视图,而不用于其他URL配置),因此为了与将来的Django版本兼容,您可以将其更改为:
from django.conf.urls import url
from accounts import views
urlpatterns = [
url(r'^register$', views.register, name='register'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', views.logout, name='logout'),
]
请注意, url()
的第二个参数不再是字符串,而是导入的views
模块中的view函数本身。
2楼
将您的应用urls.py修改为:
from django.conf.urls import patterns, url
from accounts import views
urlpatterns = patterns('', // add the empty string here
url(r'^register$', 'accounts.views.register', name='register'),
url(r'^login$', 'accounts.views.login', name='login'),
url(r'^logout$', 'accounts.views.logout', name='logout'),
)
另外,从您的主要urls.py中删除空字符串:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = [
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^admin/', include(admin.site.urls)),
]
因为我不认为使用方括号版本时需要它。