Django not appending slash to admin URL - django

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.

Related

Django flatpages catch all

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?

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')

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 url dispatcher page not found

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'),