AttributeError:'str'对象没有属性'regex'django 1.9 [英] AttributeError: 'str' object has no attribute 'regex' django 1.9
问题描述
我正在使用django 1.9,目前正在编码-在Windows命令提示符中-python manage.py makemigrations
和错误:
I am working with django 1.9 and I am currently coding - in Windows Command Prompt - python manage.py makemigrations
and the error:
AttributeError:"str"对象没有属性"regex"
AttributeError: 'str' object has no attribute 'regex'
我尝试编码:
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中,错误不断出现.
in urls.py and the error is keeps coming up.
这是我第一次在Django中进行编码,因此我的专业知识非常有限.先感谢您.
This is my first time coding in django, so my expertise is very limited. Thank you in advance.
这是整个urls.py:
This is the whole 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'),
)
推荐答案
还请确保删除开头的空网址格式-迁移网址时可能会被忽略.
Also make sure to remove the beginning empty url pattern--can be overlooked when migrating your urls.
urlpatterns = ['', # <== this blank element ('') produces the error.
...
]
tl; dr
出于好奇,我通过向django.core.checks.urls
模块中的check_pattern_startswith_slash
方法添加警告来发现这一点:
tl;dr
For the curious, I found this out by adding a warning to the check_pattern_startswith_slash
method in the django.core.checks.urls
module:
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]
当然,我收到了很多这样的警告:
And, sure enough, I got a bunch of warnings like this:
?: (urls.W002) Invalid pattern ''
这篇关于AttributeError:'str'对象没有属性'regex'django 1.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!