我正在使用django 1.9,我目前正在使用Windows命令提示符进行编码 - python manage.py makemigrations
并出现错误:
AttributeError:' str'对象没有属性'正则表达式
我尝试过编码:
url(r'^$', 'firstsite.module.views.start', name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', 'exam.views.signup', name='signup'),
url(r'^signup/submit/$', 'exam.views.signup_submit', name='signup_submit')
在urls.py中并且错误不断出现。
这是我第一次在django编码,所以我的专业知识非常有限。先感谢您。
这是整个urls.py:
from django.conf.urls import patterns, include, url
import django
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'firstsite.views.home', name='home'),
# url(r'^firstsite/', include('firstsite.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(admin.site.urls)),
django.conf.urls.handler400,
url(r'^$', 'firstsite.module.views.start', name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', 'exam.views.signup', name='signup'),
url(r'^signup/submit/$', 'exam.views.signup_submit', name='signup_submit'),
)
答案 0 :(得分:10)
首先,从urlpatterns的中间删除django.conf.urls.handler400
。它不属于那里,并且是错误的原因。
修复错误后,您可以进行一些更改以更新Django 1.8 +的代码
将urlpatterns
更改为列表,而不是使用patterns()
导入视图(或查看模块),而不是使用urls()
您对start
和login
次观看使用相同的正则表达式。这意味着您将无法访问登录视图。一个修复方法是将登录视图的正则表达式更改为^login/$
将它们放在一起,你会得到类似的东西:
from firstsite.module.views import start
from exam import views as exam_views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', start, name="home"),
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
url(r'^signup/$', exam_views.signup, name='signup'),
url(r'^signup/submit/$', exam_views.signup_submit, name='signup_submit'),
]
答案 1 :(得分:9)
还要确保删除开始的空网址模式 - 迁移网址时可能会被忽略。
urlpatterns = ['', # <== this blank element ('') produces the error.
...
]
对于好奇,我通过向check_pattern_startswith_slash
模块中的django.core.checks.urls
方法添加警告来发现这一点:
def check_pattern_startswith_slash(pattern):
"""
Check that the pattern does not begin with a forward slash.
"""
if not hasattr(pattern, 'regex'):
warning = Warning(
"Invalid pattern '%s'" % pattern,
id="urls.W002",
)
return [warning]
而且,果然,我得到了一堆像这样的警告:
?: (urls.W002) Invalid pattern ''
答案 2 :(得分:2)
删除开始的空Url
模式和
也删除
django.conf.urls.handler400,
来自urls.py
这将解决您的问题。
答案 3 :(得分:0)
对于Django 2
from django.urls.resolvers import get_resolver, URLPattern, URLResolver
urls = get_resolver()
def if_none(value):
if value:
return value
return ''
def print_urls(urls, parent_pattern=None):
for url in urls.url_patterns:
if isinstance(url, URLResolver):
print_urls(url, if_none(parent_pattern) + if_none(str(url.pattern)))
elif isinstance(url, URLPattern):
print(f"{url} ({url.lookup_str})")
print('----')
print_urls(urls)