I've been making some URLs in Django, including them and so on. Everything looks fine, but no matter what i do i always end up with a 404 on some of the simplest URLs.
For example I can browse myapp/0 abd myapp/1/details/, but i get 404'd at myapp/foo
So here are my urlconf :
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
and myapp urlconf :
urlpatterns = [
url(r'^foo/$ ', FooView.as_view()),
url(r'^(?P<bar_id>\d+)/$', BarByIdView.as_view()),
url(r'^(?P<bar_id>\d+)/details/$', BarDetailsByIdView.as_view()),
]
And when i tryp myapp/foo Django shows me the following urls list :
^admin/
^myapp/ ^foo/$
^myapp/ ^(?P<bar_id>\d+)/$
^myapp/ ^(?P<bar_id>\d+)/details/$
In the myapp.conf you have added a / at the end of the url pattern
url(r'^foo/$ ', FooView.as_view()
This must be viewed with /myapp/foo/ and not /myapp/foo because the first one matches the regex where as the second one won't.
Your URL is /myapp/foo/, not /myapp/foo. Note the trailing slash.
If you want both to work, ensure you have the APPEND_SLASH setting set to True and the CommonMiddleware enabled.
Related
It might be too simple question.
I have this urlpatterns in urls.py
urlpatterns = [
url(r'^s3direct/', include('s3direct.urls')),
path('admin/', admin.site.urls),
]
localhost/admin works, but localhost/s3direct shows the 404 error.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8099/s3direct
Using the URLconf defined in djang_test.urls, Django tried these URL patterns, in this order:
^s3direct/
admin/
The current path, s3direct, didn’t match any of these.
(I use runserver at port 8099)
The ^ in this case is a Regex-specific operator, meaning that it only matches the beginning of the string.
I believe that your problem is that you actually need to request http://localhost:8099/s3direct/ -- You're missing the trailing backslash
So I create an url in root/project/urls.py with this lines
from django.conf.urls import include
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('app.urls'))
]
while in my root/app/urls.py
from django.urls import path
from .views import UserView, AuthenticationView
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
So it is expected to give me http://localhost:8000/users/register and http://localhost:8000/users/auth urls.
Meanwhile my request doesn't behave as expected.
apparently it returns me a space between the root path and include path. I check my root/project/settings.py file I don't find any weird settings. Does anybody know what is going on?
The space is just for displaying how the URL is built up, on the debug screen only.
I have experienced the same and at first I too thought Django somehow added the space. In the end it really is that there is no match between the URL's specified and the URL in your browser. Safari doesn't display the full url, so error can happen quickly...
Some additional info on urls in Django can be found here.
The error message in your screenshot states that the request URL is http://localhost:8000/users does not exist.
Here you redirects /users/ to app.urls:
path('users/', include('app.urls'))
But in app.urls, you never included a pattern for when the URL ends with only "/users/". Instead, "/users/register/" and "/users/auth/" are both specified.
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
So http://localhost:8000/users/register and http://localhost:8000/users/auth should be valid URLs, but http://localhost:8000/users is not.
You can add another URL pattern for when the URL ends at "/users/":
urlpatterns = [
path('', AuthenticationView.as_view()), # maybe the same as /auth/ ?
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
In conclusion, Django's actually not wrong about that page does not exist (404), it's because you hadn't match http://localhost:8000/users in any of the urlpatterns.
Have you tried to use regular expressions?
path(r'^admin/', admin.site.urls)
Otherwise in the Django 2 version the urls schema has been changed, I use url function instead the path function, that can be a possible fix for your problem
I just started my journey with Django and I can't figure out what I did wrong. Sorry for that simple question.
inz/urls.py
urlpatterns = [
url(r'', include('planner.urls')),
url(r'^admin/', include(admin.site.urls)),]
planner/urls.py
urlpatterns = [
url(r'^$', views.main_page),
url(r'^/student/$', views.student, name='student'),]
And my href in base.html:
Student
And my error:
Request URL: http://127.0.0.1:8000/student/
Using the URLconf defined in inz.urls, Django tried these URL patterns, in this order:
^$
^/student/$ [name='student']
^admin/
The current URL, student/, didn't match any of these.
Remove the leading slash from ^/student/$:
url(r'^student/$', views.student, name='student'),
FYI, in the URL dispatcher docs there is a related example:
There’s no need to add a leading slash, because every URL has that. For example, it’s ^articles, not ^/articles.
I'm trying to get any sort of nested inline to work with python 3 and django 1.8.
I tried django super-inlines and that didn't really make them nested, they just kinda showed up next to eachother.
I'm currently trying django-nested-admin and I get a page not found error with this pattern search:
^ ^$ [name='browse']
^admin/ ^server-data.js$ [name='nesting_server_data']
browse links to a separate set of urls and this just will not load any admin links.
urls.py:
from django.conf.urls import include, url
import nested_admin
urlpatterns = [
url(r'^', include('flightdb.urls')),
url(r'^admin/', include('nested_admin.urls')),
]
Connecting to /admin/ gets a 404 not found error.
Any help with why the page isn't loading or any better way to get nested-inlines to work would be greatly appreciated.
The pattern r'^' will match anything that also matches r'^admin/'. Django performs the URL matching by starting from the beginning of urlpatterns and progresses until it finds the first match. So in this case things that were supposed to be in admin are matching '^' which is flightdb.urls where they won't be found. You could potentially fix this issue by reordering the url's as follows:
urlpatterns = [
url(r'^admin/', include('nested_admin.urls')),
url(r'^', include('flightdb.urls')),
]
Now admin is checked for a match before the rest of the top level urls.
EDIT: I suspect your issues are coming from using "admin" in your url pattern to go to somewhere other than the default Django admin package. I did some searching around in the source of django-nested-admin and in particular came across something here:
https://github.com/theatlantic/django-nested-admin/blob/master/nested_admin/options.py in particular have a look at line 275:
context = {
'title': _('Add %s') % force_unicode(opts.verbose_name),
'adminform': adminForm,
'is_popup': (IS_POPUP_VAR in request.POST or
IS_POPUP_VAR in request.GET),
'show_delete': False,
'media': mark_safe(media),
'inline_admin_formsets': inline_admin_formsets,
'errors': helpers.AdminErrorList(form, formsets),
'root_path': reverse('admin:index'),
'app_label': opts.app_label,
}
Here there is a call to reverse('admin:index'). This needs to resolve to the usual admin path in order to work. When you have your url's defined like you do you run into the problem of admin now being something other than the default that django-nested-admin is expecting to see.
Perhaps try a different url pattern that is not "admin" such as the one suggested by their documentation:
url(r'^nested_admin/', include('nested_admin.urls')),
This way the reverse used in the internals of the django-nested-admin package will work.
I had the same problem. This is what my settings.py and urls.py look like
settings.py
INSTALLED_APPS = [
'nested_admin',
'django.contrib.admin',
# ...
]
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^nested_admin/', include('nested_admin.urls')),
#...
]
Now, don't go to yourproject.com/nested_admin but yourproject.com/admin .
You still need the include for nested_admin or it won't work.
I'm trying to use the Django sitemap framework to set up a sitemap but I'm getting an issue with setting up the sitemap URL.
Here is my projects urls.py
from django.contrib.sitemaps.views import sitemap
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^debug-toolbar/', include(debug_toolbar.urls)),
)
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
url(r'^robots\.txt$', TemplateView.as_view(template_name="robots.txt")),
url(r'^500/$', TemplateView.as_view(template_name="500.html")),
url(r'^404/$', TemplateView.as_view(template_name="404.html")),
url(r'^', include('hunt.urls')),
)
else:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
url(r'^robots\.txt$', TemplateView.as_view(template_name="robots.txt")),
url(r'^500/$', TemplateView.as_view(template_name="500.html")),
url(r'^404/$', TemplateView.as_view(template_name="404.html")),
url(r'^', include('hunt.urls')),
)
Here is my hunt apps urls.py
urlpatterns = patterns('hunt.views',
url(r'^$', top100, name='top100'),
url(r'^explore/$', explore, name='explore'),
url(r'^monthly/$', monthlytop10, name='monthlytop10'),
url(r'^trending/$', trending, name='trending'),
url(r'^genres/$', genres, name='genres'),
url(r'^genres/(?P<slug>.+)/$', genre_view, name='genre_view'),
url(r'^contact/$', contact, name='contact'),
url(r'^contact/thanks/$', thanks, name='thanks'),
url(r'^faq/$', faq, name='faq'),
url(r'^songs/(?P<slug>.+)$', song_search, name='song_search'),
url(r'^loadmore/dj/$', loadmore_dj, name='loadmore_dj'),
url(r'^loadmore/genre/$', loadmore_genre, name='loadmore_genre'),
url(r'^loadmore/month/$', loadmore_month, name='loadmore_month'),
url(r'^loadmore/trending/$', loadmore_trending, name='loadmore_trending'),
url(r'^vote/dj/$', vote_dj, name='vote_dj'),
url(r'^vote/genre/$', vote_genre, name='vote_genre'),
url(r'^vote/month/$', vote_month, name='vote_month'),
url(r'^vote/trending/$', vote_trending, name='vote_trending'),
url(r'^(?P<slug>.+)/$', dj, name='dj'),
)
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
When I try to access sitemap.xml I'm getting an error
404 Page Not Found
No DJ matches the given query.
This means it tried to access the last URL in my hunt app. Why is it trying to access the DJ Page even though it comes after sitemap.xml URL?
EDIT -
As per eran's suggestion, I ran the command show_urls of django_extensions. Here is the output of that
/ hunt.views.top100 top100
/404/ django.views.generic.base.TemplateView
/500/ django.views.generic.base.TemplateView
/<slug>/ hunt.views.dj dj
/contact/ hunt.views.contact contact
/contact/thanks/ hunt.views.thanks thanks
/explore/ hunt.views.explore explore
/faq/ hunt.views.faq faq
/genres/ hunt.views.genres genres
/genres/<slug>/ hunt.views.genre_view genre_view
/loadmore/dj/ hunt.views.loadmore_dj loadmore_dj
/loadmore/genre/ hunt.views.loadmore_genre loadmore_genre
/loadmore/month/ hunt.views.loadmore_month loadmore_month
/loadmore/trending/ hunt.views.loadmore_trending loadmore_trending
/media/<path> django.views.static.serve
/monthly/ hunt.views.monthlytop10 monthlytop10
/robots.txt django.views.generic.base.TemplateView
/sitemap.xml django.contrib.sitemaps.views.sitemap
/songs/<slug> hunt.views.song_search song_search
/static/<path> django.contrib.staticfiles.views.serve
/trending/ hunt.views.trending trending
/vote/dj/ hunt.views.vote_dj vote_dj
/vote/genre/ hunt.views.vote_genre vote_genre
/vote/month/ hunt.views.vote_month vote_month
/vote/trending/ hunt.views.vote_trending vote_trending
According to that show_urls output, the URLpattern for hunt.views.dj is taking precedence, because the r'^(?P<slug>.+)/$' regex appears before the sitemap.xml regex. The former regex will match a request to "sitemap.xml", so Django will never reach the pattern that's specifically meant for sitemap.xml. For that matter, it will never reach /contact/, /explore/, /faq/, etc.
I suspect that's happening because your project-wide urls.py appends to urlpatterns ("urlpatterns +=") rather than setting it directly. Looks like you didn't paste all of the imports from the project-wide urls.py, but I would assume one of the things you're importing includes a patterns object; otherwise you'd be getting a NameError at import time (name 'patterns' is not defined).
You should be able to fix it by changing this...
urlpatterns += patterns('',
...to this...
urlpatterns = patterns('',
But I have no way of knowing for certain, because I don't know what else you've imported in the project's urls.py.
I had a sitemaps.xml file in my templates folder that I was using before. Deleting that solved the issue.
I notice that your sitemap and robots.txt urls are the only ones that don't end in a trailing slash. Many web servers will redirect myurl.com/item to myurl.com/item/ - for example, this is the preferred practice when using Apache (search for "Trailing Slash Problem"). Try doing a curl to the url without a trailing slash - do you get a 301 redirect? If so then it is likely redirecting to the URL which DOES include a trailing slash, which is not matched by a pattern in your urls.py. Try adding trailing slashes to your regular expressions, or setting APPEND_SLASH = True in settings.py