Django url dispatcher page not found - django

i am trying to define a url pattern in django urls.py like
url(r'^networking$','mysite1.networking.views.networking'),
when i am typing http://myhost.com/networking in my address bar to go to networking page
i am getting 404 error and a slash '/' automatically added to the address bar like
http://myhost.com/networking/
help me out what i am doing wrong?

You probably aren't including your urlconf correctly. The behavior you're seeing is because of APPEND_SLASH is set to True by default when Django can't resolve the url.

Either set Append_Slash to false which is true by default or use your url description like given below which redirect url with slash to desired view.
url(r'^networking/$','mysite1.networking.views.networking'),

Seems your Apache server or some Django middleware is adding trailing slashes. You can either correct that, or the better way is you can use the following url pattern:
url(r'^networking/?$','mysite1.networking.views.networking'),

Related

DRF post url without end slash

In my application , I need to display the REST url(s) without a slash at the end. I have tried below combination but it didnt work.
Added APPEND_SLASH=True in the settings.py
and on the urls.py file
from rest_framework.routers import SimpleRouter
router = SimpleRouter(trailing_slash=False)
After adding this when I am calling the urls without slash at the end in the postman, it is giving me an 404 error- URL not found. But with slash at the end is working fine.
Is there any option to make this url without with slash at the end ? Especially for the post urls
APPEND_SLASH will append it to the request (e.g. mysite/blog --> mysite/blog/). This is not what you want, since your urlconf explicitly says there should be no slash.
Also APPEND_SLASH is True by default. So you need to set it to False instead. That way, if you make a request without a slash, Django won't automatically add in a slash.

Django - issue with APPEND_SLASH and POST requests

I am using Django 1.10, and my goal now is to make urls available both with and without trailing slash. To do this, I added slash to all my URLs in the URLConf files, and then set APPEND_SLASH variable value to True (well, this is the default value).
Now the problem is that external POST requests (which I can't control) yield the following error:
You called this URL via POST, but the URL doesn't end in a slash and
you have APPEND_SLASH set. Django can't redirect to the slash URL
while maintaining POST data. Change your form to point to
127.0.0.1:8000/Calendar/AddAccounts/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
They mention this in Django doc, and yet after hours of surfing the net, I can't figure out how to address this issue.
I've also come across this question, but the proposed solution doesn't seem acceptable to me. It says I have to make the users call my URLs only with trailing slash. While I know that in other languages (C# for example) it is possible to enable both options
It seems weird to me that you want to support both cases. Ideally you would want to redirect from non slash to slash(or the other way around if you want that) on the server level (nginx/apache/whatever you use) before the request hits Django.
Just choose a strategy and stick to it, so add the trailing slash to your form and never look back. :)
It's important to be consistent. https://www.branded3.com/blog/urls-trailing-slash-seo/
If the urls are used for APIs or the SEO is not important to you, you can consider both with slash and without slash by adding "/?". In django 3.X:
from django.urls import re_path
re_path(r'^query/?$', 'search.views.query'),
re_path(r'^add/?$', 'search.views.add'),
In Restframework routers:
from rest_framework.routers import DefaultRouter
class CustomDefaultRouter(DefaultRouter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.trailing_slash = '/?'
router = CustomDefaultRouter()
router.register('link', ViewSet, basename='link')

404 on a simple template view - strange

I have a django app set up consisting of ListViews, TemplateViews etc..
So, I just added a small templateview to it like so:
#views.py
class TermsTemplateView(TemplateView):
template_name = "terms.html"
#urls.py
url(r'^terms/$', TermsTemplateView.as_view(), name='terms'),
and in terms.html, I am using for linking:
Terms & Conditions
For some strange reason, I keep getting 404 on localhost/terms as follows:
404: No <model_name> found matching the query
I am baffled why this is happening all of a sudden. I have the same set up for "about", "thanks", "contact" pages, and they seem to display it with no problems.
..and the worst part is, if I modify the urls.py like so:
url(r'^/terms/$', TermsTemplateView.as_view(), name='terms'),
and then go to http://127.0.0.1:8000//terms/ - the page seems to be there.. I am surprised why this is so :(
Any help would enlighten me!
The / at the end is the culprit of your problems. localhost/terms doesn't match '^terms/$' regular expression, localhost/terms/ does.
You can make / at the end optional by using ?:
url(r'^terms/?$', TermsTemplateView.as_view(), name='terms'),
UPD: Note that there is a better solution to the problem, APPEND_SLASH:
When set to True, if the request URL does not match any of the
patterns in the URLconf and it doesn’t end in a slash, an HTTP
redirect is issued to the same URL with a slash appended.
Also see:
Why would you need a slash at the end of a URL?
django - url with automatic slash adding
Append Slashes to URLs in Django

Whats the difference between the 2 url routes in Django?

In Django, we define url mapping in urls.py. Whats the difference between these two?
/app/kill/$
/app/kill$
They are both very different urls. But for must users they will be expected to be the same so django has a nice feature for this: the APPEND_SLASH settings variable.
From the docs:
When set to True, if the request URL does not match any of the patterns in the URLconf and it doesn't end in a slash, an HTTP redirect is issued to the same URL with a slash appended.

Django- trying to use APPEND_SLASH and a custom catchall 404 view

I generally add a catch-all 404 regex to my Django websites as the last expression in my urls.py:
urlpatterns += patterns('django.views.generic.simple',
(r'^.', 'direct_to_template', {'template': 'unknown.html'}),
I'm generally happy with the performance of this. The unknown.html template extends my base template and nicely tells the viewer that their entered url doesn't exist, but the page still has all the navigation and style of my website.
However, after having to repeatedly tell people to enter a trailing slash, I feel that the APPEND_SLASH = True parameter in settings.py needs to be set.
the docs state:
If APPEND_SLASH is True and the
initial URL doesn’t end with a slash,
and it is not found in the URLconf,
then a new URL is formed by appending
a slash at the end. If this new URL is
found in the URLconf, then Django
redirects the request to this new URL.
Otherwise, the initial URL is
processed as usual.
So following this logic, foo.com/bar is successfully caught by my "404" url expression before it can be redirect to my foo.com/bar/ url expression.
*What is the best way to maintain a friendly/custom catchall 404 page while also being able to use APPEND_SLASH or something with similar functionality?*
--edit/answer--
Somehow I missed that you just need to add a template named 404.html, and also make sure DEBUG = False
Thanks DTing!
I think you can just customize your 404.html instead of using a "catchall" since you are just redirecting to a custom template. There is no reason why your custom 404.html template can't extend your site's base.html.
http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views
Three things to note about 404 views:
The 404 view is also called if Django doesn't find a match after
checking every regular expression in
the URLconf.
If you don't define your own 404 view -- and simply use the default,
which is recommended -- you still have
one obligation: you must create a
404.html template in the root of your template directory. The default 404
view will use that template for all
404 errors. The default 404 view will
pass one variable to the template:
request_path, which is the URL that
resulted in the 404.
The 404 view is passed a RequestContext and will have access to
variables supplied by your
TEMPLATE_CONTEXT_PROCESSORS setting
(e.g., MEDIA_URL).
If DEBUG is set to True (in your settings module), then your 404 view
will never be used, and the traceback
will be displayed instead.
if you do want to use a custom view,
This page_not_found view should
suffice for 99% of Web applications,
but if you want to override the 404
view, you can specify handler404 in
your URLconf, like so:
handler404 = 'mysite.views.my_custom_404_view'