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

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.

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

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 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 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.