gpt4 book ai didi

python - 属性错误 : 'str' object has no attribute 'regex' django 1. 9

转载 作者:太空狗 更新时间:2023-10-29 17:57:08 25 4
gpt4 key购买 nike

我正在使用 django 1.9,目前正在编写代码 - 在 Windows 命令提示符中 - python manage.py makemigrations 和错误:

AttributeError: 'str' 对象没有属性 '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 中,错误不断出现。

这是我第一次在 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'),
)

最佳答案

还要确保删除开头的空 url 模式——在迁移 url 时可能会被忽略。

urlpatterns = ['', # <== this blank element ('') produces the error.
...
]

tl;dr

出于好奇,我通过向 django.core.checks.urls 模块中的 check_pattern_startswith_slash 方法添加警告发现了这一点:

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 ''

关于python - 属性错误 : 'str' object has no attribute 'regex' django 1. 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com