11👍
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
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 ''
10👍
Firstly, remove the django.conf.urls.handler400
from the middle of the urlpatterns. It doesn’t belong there, and is the cause of the error.
Once the error has been fixed, you can make a couple of changes to update your code for Django 1.8+
-
Change
urlpatterns
to a list, instead of usingpatterns()
-
Import the views (or view modules), instead of using strings in your
urls()
-
You are using the same regex for the
start
andlogin
views. This means you won’t be able to reach the login views. One fix would be to change the regex for the login view to something like^login/$
Putting that together, you get something like:
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'),
]
- Django ajax error response best practice
- Django CSRF Failed: CSRF token missing or incorrect
- Amazon + Django each 12 hours appears that [Errno 5] Input/output error
2👍
Remove the beginning empty Url
patterns and
also remove
django.conf.urls.handler400,
from your urls.py
this will solve your problem.
- Django_auth_ldap no module named ldap
- My Django installs in virtual env are missing admin templates folder
- AWS Elastic Beanstalk health check issue
- <django.db.models.fields.related.RelatedManager object at 0x7ff4e003d1d0> is not JSON serializable
0👍
For 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)