django urlpatterns order - python

I ran into a problem loading the admin section of my django site with the following error:
No FlatPage matches the given query.
I managed to solve the problem, but I'm attempting to understand why it solved the problem. In my urls.py file, I moved the url(r'', include('django.contrib.flatpages.urls')), after the url(r'^admin/', include(admin.site.urls)), and voila, it worked. Can someone explain, briefly, why this would make a relevant difference? Should the admin url always be at the top of the list?
Code the produced the error:
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
url (r'^blog/', include('blog.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'', include('django.contrib.flatpages.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
This is django 1.4.

The problem is that you have not installed the flatpages application correctly. Django's flatpages application relies on a piece of middleware that intercepts 404 requests. As a result, you do not need to add django.contrib.flatpages.urls to your urls.py.
You received that error because the regex you are using ('') matches all URLs. As a result, the urlpattern never reached ^admin/'.

Related

AttributeError: 'str' object has no attribute 'regex' django 1.9

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' object has no attribute 'regex'
I have tried coding:
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')
in urls.py and the error is keeps coming up.
This is my first time coding in django, so my expertise is very limited. Thank you in advance.
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
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 ''
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 using patterns()
Import the views (or view modules), instead of using strings in your urls()
You are using the same regex for the start and login 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'),
]
Remove the beginning empty Url patterns and
also remove
django.conf.urls.handler400,
from your urls.py this will solve your problem.
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)

requested url not found with production setting in django

My website runs with no problem when DEBUG is set to true. but i get requested url not found error when i run the server in production setting. the admin page is fine.
this is my project urls.py:
urlpatterns = patterns('',
url(r'^', include('photography.urls')),
url(r'^admin/', include(admin.site.urls)),
# url(r'^$', 'photography.views.hello_world', name='home'),
)
this is my app urls.py:
urlpatterns = patterns('photography.views',
url(r'^$', views.index, name='index'),
url(r'^albums/(?P<slug>[a-zA-Z0-9-]+)/$', views.photos_by_location, name='photos_by_location'),
)
For testing purpose, i added a hello world view and it gets rendered with commented line in project urls.py. so I guess my way of including app urls is not really good when DEBUG is false . Is there a way to include my app urls that works in production setting?
Any help would be appreciated. thanks.
Update:
tried url(r'^/', include('photography.urls')), with a / after ^. didnt work either.

The included urlconf doesn't have any patterns in it + django

I have been trying out the django tutorial and stuck with this error. I am in the 3rd page of the tutorial and everything was going good until i encountered this error Django Tutorial. I was following the exact steps in the tutorial. This is the error i got
ImproperlyConfigured at /polls
The included urlconf <module 'polls.urls' from 'C:\\Python34\\mysite\\polls\\urls.py'> doesn't have any patterns in it
I am pasting my code here.
ROOT_URLCONF = 'mysite.urls'` in settings.py,
from django.conf.urls import patterns, url
from polls import views
urlpattern = patterns('',
url(r'^$',views.index,name='index')
)`
inside MYSITE/POLLS/URLS.PY
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
INSIDE MYSITE.URLS.PY
Sorry if i missed out anything that i had to mention to get a clear picture. Any help would be appreciated.
The first file has urlpattern instead of urlpatterns.

404 when enabling admin with Django

I had correctly configured a Django app and had it running smoothly. However, when I went to enable the administration panel, I encountered a 404 error upon uncommenting this line:
url(r'^admin/', include(admin.site.urls))
Upon trying to access my site, I received this:
Using the URLconf defined in cms.urls, Django tried these URL patterns, in this order:
1. ^admin/
The current URL, , didn't match any of these.
I can't figure out how to get around this error. Does anyone with some Django experience have a solution? Thanks!
EDIT: Here is the entire urls.py file:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'cms.views.home', name='home'),
# url(r'^cms/', include('cms.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)),
)
Have you tried, http://yourdomain:port/admin ? If you are using the default url, I would try
http://127.0.0.1:8000/admin

django 1.3 admin problem

everytime i click on any link in django admin, it will call other urls too, and log me out, so now i can't change anything.
session id is changed everytime too.
i've checked urls.py and the admin ones are at the top of the file.
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'developer.views.home', name='home'),
# url(r'^developer/', include('developer.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)),
)

Categories

Resources