Django flatpages catch all - django

I am using the catch all pattern for flatpages in Django, like this:-
urlpatterns = [
path('somepath/', include('project.somepathapp.urls')),
path('anotherpath/', include('project.anotherpathapp.urls')),
etc.
]
urlpatterns += [
path('<path:url>/', views.flatpage),
]
In my template, I use:-
About Us
to get to the About flatpage. But the URL pattern strips off the final slash and passes the flatpage view the URL /about-us (as opposed to /about-us/). The flatpage view then spots that the URL doesn't end in a slash, adds it back in, finds the page and redirects to that URL but adds an extra slash so that the URL is now /about-us//
If I remove the final slash from the catch all pattern, any URL from the main paths (somepath/ and anotherpath/) without the final slash is matched by the catch all pattern before the APPEND_SLASH is used and, since there is no page with that URL, the user gets a 404. So a URL like /somepath/ will work but /somepath won't.
What have I done wrong? It seems like a catch-22 to me. I can't use the middleware option because that doesn't always get passed through other middleware views so I'm stuck.
Any ideas?

Related

Django not appending slash to admin URL

I'm using Django for my project's backend, and React on the frontend. As such, I have to specify a fallback URL which serves up index.html, which contains the script for all of my frontend code.
However I want Django to match admin and admin/. The Regex below solves the problem, but accessing admin doesn't append a slash (I have APPEND_SLASH set to True in settings). The result is ugly looking urls like adminapis instead of admin/apis when I am using the admin interface.
Any idea how I can preserve the trailing slash?
urlpatterns = [
re_path(r"admin/?", admin.site.urls),
path("", include("api.urls", namespace="api")),
re_path(r".*", TemplateView.as_view(template_name="index.html")),
]
Your url pattern should be r"admin/", without the question mark, i.e. make the trailing slash mandatory.
Then verify that in your settings.py
there is no APPEND_SLASH=False (You want the default value True)
and that you have 'django.middleware.common.CommonMiddleware' in
the MIDDLEWARE tuple (needed for
APPEND_SLASH).
Now, when you enter the url […]/admin, a trailing slash will automatically be added.
If you don’t remove the question mark, django will not add the trailing slash, because the url is valid without it.

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.

the current path ,About.html didn't match any of these in django

from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^About/', views.About,name='About'),
url(r'^checkout/', views.checkout,name='checkout'),
url(r'^contact', views.contact,name='contact'),
url(r'^faqs', views.faqs,name='faqs'),
url(r'^help', views.help,name='help'),
url(r'^icons', views.icons,name='icons'),
url(r'^payment', views.payment,name='payment'),
url(r'^privacy', views.privacy,name='privacy'),
]
The error message:
Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/About.html
Using the URLconf defined in shop.urls, Django tried these URL patterns,
in this order:
admin/
^$ [name='index']
^about/$ [name='about']
^checkout/$ [name='checkout']
^contact/$ [name='contact']
^static\/(?P<path>.*)$
The current path, About.html, didn't match any of these.
This kind of an error could occur from 2 or 3 different scenarios.
In your case, you seem to put the wrong URL in the browser address bar.
Your correct URL should be http://127.0.0.1:8000/About (as you've written in the URL patterns).
Remember, About.html - is the HTML template you create inside the templates folder. Even though you route to the html page (with a string like: app_name/About.html) - the actual URL in the address bar will be according to what you write in the regex path r'^url_name'. If you write r'^About.html' in url patterns, then http://127.0.0.1:8000/About.html should work perfectly.
The second scenario (based on my experience) which could produce this type of an error is when you forget to pass the 'request' argument inside the method that defines view of the URL - in the respective views.py file.
You should be having a method named About which would look like this in views.py
def About(request):
return render(request,'app_name/About.html')
If you forget to pass argument in the paranthesis of About, this kind of an error could occur.
Finally, if you are using django 2, please start using re_path method to serve regex url patterns. The url method is likely to be depracated in future release.
Refer re_path documentation.
your URL will not be http://127.0.0.1:8000/About.html it will just be http://127.0.0.1:8000/about (remember urls are case insensitive), this will take you to your view which is named About, in your view you should reference your template in its render (about.html)
have you read the my first Django app https://docs.djangoproject.com/en/2.0/intro/tutorial01/ its a great place to start if you are unfamiliar with how django operates
What you are trying to hit is not a valid url, you have to hit http://127.0.0.1:8000/About as written in urls.py.
You have to understand the difference between urls and html templates, this About.html would be used in views while rendering like:
return render(request, 'your_app/about.html')
And for sure you can write a url if you want like this:
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^About.html/', views.About,name='About'),
.
.
]
Check the documentation
The url which you provide in url ( ) method doesn't contain any suffix of .html
You can goto the about page directly by /About

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

Django "Page not found" error page shows only one of two expected urls

I'm working with Django, admittedly for the first time doing anything real.
The URL config looks like the following:
urlpatterns = patterns('my_site.core_prototype.views',
(r'^newpost/$', 'newPost'),
(r'^$', 'NewPostAndDisplayList'), # capture nothing...
#more here... - perhaps the perma-links?
)
This is in an app's url.py which is loaded from the project's url.py via:
urlpatterns = patterns('',
# only app for now.
(r'^$', include('my_site.core_prototype.urls')),
)
The problem is, when I receive a 404 attempting to utilize newpost, the error page only shows the ^$ -- it seems to ignore the newpost pattern...
I'm sure the solution is probably stupid-simple but right now I'm missing it. Can someone help get me on the right track...
Your pattern for the include will only match an empty URL string, change it to a prefix which should be mapped to the included urls, or remove the $ from that pattern.