Django: URL Path not found jumps into next app - django

I have two apps:
backend
shop
I my urls in main app dir:
path('backend/', include('backend.urls')),
path('', include('shop.urls')),
the problem is if I write in my url: localhost:8000/backend/abc which not exist Django jumps over to shop.urls and the app is crashing because it can not find the slug and the query goes in fail.
How can I prevent if I go to the url /backend/somethingwhichnotexist is returning an 404 and not search in other app urls for this folder? I have thought that this is one of the main reason for split the urls in app folders.
Here are some urls from backend/urls.py:
from django.urls import path, re_path
from . import views as backend_views
from django.contrib.auth import views as auth_views
from froala_editor import views
from django.conf.urls import include
urlpatterns = [
path('stamdata/', backend_views.edit_masterdata),
path('praefikser/', backend_views.edit_prefixes),
path('leverandorer/', backend_views.suppliers_view),
path('leverandorer/add', backend_views.add_supplier),
]
handler404 = 'backend.views.page_not_found_view'
shop/urls.py
here the url stops:
path('<slug:category_slug>/<slug:slug_subcategory>/', butik_views.cat_or_article),
normally I don't want that a backend urls switches to frontend view
regards
Christopher.

You you said Django run through all of the URL patterns and stops till it finds its matching URL. SO, what has happen here is that when you type a URL with localhost:8000/backend/abc/ it runs through all the URLS before returning 404 to the client. So, it stops at the URL that support any string with 2 parameters in your case below URL.
path('<slug:category_slug>/<slug:slug_subcategory>/', butik_views.cat_or_article),
To get a 404 by adding a static word in you URL.
path('shop/<slug:category_slug>/<slug:slug_subcategory>/', butik_views.cat_or_article),
or
path('backend/', include('backend.urls')),
path('shop/', include('shop.urls')),

For now I fixed it with
shop/views.py
if category_slug == 'backend':
response = render(
request,
'backend/404.html',
)
response.status_code = 404
return response
else:
until I found another solution. It looks like that the URL dispatcher is working like this:
Django runs through each URL pattern, in order, and stops at the first
one that matches the requested URL, matching against path_info.
So for now I did not found another way.
Edit:
I added this in the end of backend/urls.py:
re_path(r'^.*/$', backend_views.page_not_found_view)
so after all working urls it goes to all other to the 404 view.

Related

Django flatpages The empty path didn't match any of these

On the administration page, I set the URL for the Django flat pages to "/", which is expected to be displayed as the home page at http://127.0.0.1:8000/. Doing so I encountered an error:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
admin/
<path:url>
The empty path didn't match any of these.
But if I go to http://127.0.0.1:8000// with double slash then the home page is displayed correctly. My only urls.py file looks like this:
from django.contrib import admin
from django.urls import include, path
from django.contrib.flatpages import views
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += [
path('<path:url>', views.flatpage),
]
And I took all the code completely from the official guide. How to display the django flatpage homepage at http://127.0.0.1:8000/?
In addition to the django.urls.path method Django offers the django.urls.re_path method. The path method is designed to perform matches against exact strings, whereas the re_path method is designed to perform matches against patterned strings based on regular expressions. In my case it’s enough to fix it this way:
urlpatterns += [
re_path(r'^(?P<url>.*)$', views.flatpage),
]
As a result, we get the correct processing of requests at http://127.0.0.1:8000/. More details about using path, re_path methods can be found at the link.

How url dispatcher include apps urls

I have project urls.py
from django.contrib import admin
from django.urls import path,include
from django.contrib.auth import login
urlpatterns = [
path('admin/', admin.site.urls),
path('login/',include
('authorization.urls')),
]
My included authorization/urls.py file is followed
from django.urls import path
from . import views
urlpatterns = [
path('login',views.login_name),
]
While i started to learn url dispatcher logic i much times faced with
The current path, login/login/, didn't
match any of these.
As pointed in documentation
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
When i am trying to access my login page i see
Using the URLconf defined
in forecast.urls, Django tried these
URL patterns, in this order:
admin/
login/ login
The current path, login/, didn't match any of these.
As i see from traceback and from documentation notes while inside path function included include() function. Path parse given arguments and by examplefrom my project first of all path parse given url pattern login than when path face with include function its give parsed url pattern to included urls. Than when included urls.py parsed its face with another url pattern login its chain them login/login and give it to attached views function.
Here is my first question how i should specify url pattern while i need use include function
Am i understood correctly in included app.urls.py i shouldn't specify login pattern based on my example
Can anyone understand me how path include function work that i can debug it
you have repeated 'login', so change it as follows, so it will be available on /login
path('',include('authorization.urls')),

Page not found, URL not resolved

I am trying to call a function in my views.py by pasting a url http://127.0.0.1:8000/upload_results/UniEX_HG1_A15 in the web browser,
but the request fails and I can not see why my URL pattern does not work.
The error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/upload_results/UniEX_HG1_A15
Using the URLconf defined in varview.urls, Django tried these URL patterns, in this order:
^$ [name='show_index']
^admin/
^upload/ [name='varview_submission']
^upload_results/(?P<project_id>[0-9A-Za-z_]+)/ [name='varview_upload_results']
^validate/(?P<project_id>[0-9A-Za-z_]+)/ [name='varview_validate']
^filterallprojects/[0-9A-Za-z_]+ [name='varview_filterallprojects']
^project/(?P<project_id>[0-9A-Za-z_]+)/wgsmetrics/ [name='varview_wgsmetrics']
^project/(?P<project_id>[0-9A-Za-z_]+)/targetgenecoverage/ [name='varview_targetgenecoverage']
^project/(?P<project_id>[0-9A-Za-z_]+)/(?P<display_option>[0-9A-Za-z_]+)/ [name='varview_project']
^media\/(?P<path>.*)$
The current path, upload_results/UniEX_HG1_A15, didn't match any of these.
And here is my urls.py:
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from varview import views
from varview.forms import DataUploaderForm1, DataUploaderForm2, GetProjectIdForm
urlpatterns = [
url(r'^$', views.show_index, name='show_index'),
url(r'^admin/', admin.site.urls),
url(r'^upload/', views.init_submission, name='varview_submission'),
url(r'^upload_results/(?P<project_id>[0-9A-Za-z_]+)/', views.upload_results, name='varview_upload_results'),
]
This already worked some time ago, but in the meantime I did many changes.
Latest change was to include celery (djcelery).
The index page and others still work. I already read many posts related to django-url, but could not figure it out.
Thank you for your help.
Note your URL has a trailing slash,
^upload_results/(?P<project_id>[0-9A-Za-z_]+)/
but you are trying to access a URL without the trailing slash
/upload_results/UniEX_HG1_A15
Normally, Django will redirect to the URL with the trailing slash. Perhaps your MIDDLEWARE setting is incorrect, or you have set APPEND_SLASH to False.

django url not working when directions followed

This is driving me crazy. Everything looks good. I am getting this error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/home
Using the URLconf defined in gds.urls, Django tried these URL patterns, in this order:
^admin/
^fixiss/
The current URL, home, didn't match any of these.
Here is my root url:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^fixiss/', include('fixiss.urls')),
]
My app url:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name="index"),
]
And the view in my app:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Home page!")
Any help would be greatly appreciated!
Assuming your "app url" module is 'fixiss.urls' where you only have one pattern (the empty string) and you are you are including it under fixiss/, the only match should be:
http://localhost:8000/fixiss/
If you change your one pattern to:
url(r'^home$', views.home, name="index")
that view will be served under
http://localhost:8000/fixiss/home/
The actual name of the view function (home in this case) is rather irrelevant when it comes to url pattern matching. What counts is the specified regex pattern.
This is very well documented:
Django url dispatching in general
Including urls in particular
That is because no url matches home/. Your url should be http://localhost:8000/fixiss/
In the app's (fixiss) url file, the regex is empty meaning, it does not expect a string after fixiss/ in the url for it to match.

Trouble setting up django settings

I'm just learning Django and trying to setup the View and URLconfs (http://djangobook.com/en/2.0/chapter03/).
Inside my project folder "mysite" (/Users/NAME/Desktop/development/Python/djcode/mysite), I have the following two files:
views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
and urls.py
from django.conf.urls.defaults import *
from mysite.views import hello
urlpatterns = patterns('',
(r'^hello/$', hello),
)
However, when I run the test server, it shows a 404 page saying:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^hello/$
The current URL, , didn't match any of these.
I think this has to do with my settings.py not being correct. What do I need to change in the settings.py file to point it to the correct destination?
You have no urlconf pattern corresponding to the root of your webserver. Add ^$ and make it go somewhere.
Looks like the URL you entered when testing it was "http://localhost:8000/". You should enter "http://localhost:8000/hello/" to see the output of the function you made.