How url dispatcher include apps urls - django

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

Related

Django URL order

iam stuck again I hope I will get useful help this time too.
Iam trying to run the app but it gives me URL configuration error something like this:
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I see a similar query in this platform but I couldn't find my answer
Iam using Django version 2.1
My code in pyshop.urls is:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
You don't route / anywhere, and
The empty path
implies that's exactly where you're visiting.
You'll either need to
add e.g. path('', SomeHomepageViewMaybe),
or remove the /products/ prefix to make the products app a "main" app (not that there's a concept like that in Django),
or just navigate to /products/ instead

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.

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 Tutorial Part 1 Error: URL does not match URL patterns

I am going throught the Django tutorial here:
https://docs.djangoproject.com/en/1.10/intro/tutorial01/
I follow the instructions exactly. But when I try to go to http://localhost:8000/polls/, I don't see the message “Hello, world. You’re at the polls index.” as expected. Instead, I get the following error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, polls/, didn't match any of these.
Here is my mysite/urls.py file. I am not sure why the first regex pattern in urlpatterns is not recognized.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
You might have forgot .html extension in views.py while requesting the page.

Django URL-Patterns not working as expected when using INCLUDE

I am currently working my way through the Django Tutorial (Step 3) and am stuck at the part with "Decoupling the URLconfs".
What I try to do is to set up one URL-Pattern that catches lnadmin/, to redirect to the django admin, and eventually another catch-all that redirects to other patterns included from another file.
Here's my mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^lnadmin/', include(admin.site.urls)), #match admin
url(r'^test/', include('lnapp.urls')), #match test, should be a catch-all later
)
and here's the lnapp/urls.py, which is supposed to match hash/(anything)/:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('lnapp.views',
url(r'^hash/(?P<hash>.+)/$', 'hash'), #match part to load from hash
)
I had this pattern in the main url.py before, and it worked as intended.
What's happening now is that when I open (mydomain)/lnadmin/, it tries to access lnapp.views.hash (Could not import lnapp.views.hash, as no view is defined yet).
This doesn't make any sense to me, as lnadmin/ should be matched by the first pattern, and /lnadmin/ doesn't match test/hash/(anything)/. As soon as I comment out the one url in lnapp/urls.py, it redirects to the admin, as intended.
Swapping both urls in the main url.py has no effect.
The answer to my own question is: you have to define a view even for the unmatched urls, or else it will fail.