Django: Page not found but path is shown - python

I have recently started using Django and I am a little lost as to why my services.html page does not render as I have followed what appears to be the same steps I took for my home page to work but I keep getting the error that nothing matches the current path. Below are my snippets for views and urls.py. I also had it be included in my website urls.py by using path('', include('affiliate.urls')),
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
[name='services']
The current path, services.html, didn't match any of these.
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.services, name="services"),
]
Views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html', {})
def services(request):
return render(request, 'services.html', {})

Change your urlpatterns as follows:
urlpatterns = [
path('', views.home, name="home"),
path('services/', views.services, name="services"),
]
Now when you want to access the services view, make sure to use 127.0.0.1:8000/services
because both of your paths for home and services are pointing to the same url.
Willem is trying to explain you the same thing.

Related

Django FirstApp

I am following the django tutorial (found here):
views.py code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
urls.py code (this is inside polls app urls.py file):
from django.urls import path, include
from django.conf import settings
from . import views
urlpatterns = [path(r'^$', views.index, name='index'), ]
urls.py code ( this is root urls.py file code):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('polls/', include('polls.urls')),path('admin/',admin.site.urls), ]
Here is my run command : python manage.py runserver 8080
I tried to run it today, but I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://35527a91f40c4e228d6c464d8a8c8487.vfs.cloud9.eu-west-1.amazonaws.com/
Using the URLconf defined in PollApp.urls, Django tried these URL patterns, in this order:
poll/
admin/
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.
You are using regex-syntax with path, you should use the empty string instead:
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Furthermore, you use a poll/ prefix here, so that means you either need to visit localhost:8000/poll/, or change this to an empty prefix:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Djano errors are self-explanatory, follow its instructions, and are good to go.Also is there the Indentation in your first return ?
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

Django page not found, help would be appreciated

I was taking a youtube tutorial on making a basic website using django and I got this error when coding:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
app/
admin/
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.
This is my code:
For urls.py/mysite:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
For views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world!")
For urls.py/myapp:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
You need to add root level path to access the path you specified http://127.0.0.1:8000/:
urls.py/mysite
urlpatterns = [
path('', views.index, name='home'),
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
views.py/mysite
def index(request):
return HttpResponse('This is home page')
Since you specified:
path('app/', include('myapp.urls')),
It means all the paths in myapp.urls are prefixed with app/. So you can access the index view with: http://127.0.0.1:8000/app/. Or if you want to access the index view with http://127.0.0.1:8000/ you rewrite the path to:
path('', include('myapp.urls')),

Routing with Django and React

I'm very new to working with Django and I've been relying on some tutorials to link this one to React, but the problem is that initially (when I open 127.0.0.1:8000) React loads the routes perfectly, then when I reload the page Django tries to interpret the path from urls.py and obviously can't find it.
The error is:
Page not found (404) Using the URLconf defined in memberstack.urls, Django tried these URL patterns, in this order:
admin/
api/token-auth/
core/
I hope you can help me, thanks in advance
my_project/urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import obtain_jwt_token
from frontend.views import index
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
]
frontend/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
frontend/views.py
from django.shortcuts import render
def index(request):
return render(request, 'frontend/index.html')
For this, you'll have to use a catch-all in order for React to handle all the routing instead of django there.
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
re_path(r'^(?:.*)/?$', include('frontend.urls')),
]
Or
urlpatterns = [
path('', views.index),
re_path(r'^(?:.*)/?$', views.index)
]
I think the better practice would be to implement Django-Rest-Framework and build them separately.

The current path, home/, didn't match any of these

I have two apps on my project- one is accounts and home
home app url - http://localhost:8000/home/ is rising an error that the
current path, home/, didn't match any of these.
I'm using django version 2.2.4
and i'm using regular expression on my home url pattern
My home app url
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home')
]
My home view
view.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('It worked !..')
Main url
urls.py
urlpatterns = [
path(r'^admin/', admin.site.urls),
url(r'^$', views.login_redirect, name='login_redirect'), # automatically takes to login page
path(r'^account/', include(('accounts.urls','accounts'), namespace='accounts')),
path(r'^home/', include(('home.urls','home'), namespace='home')),
path('', include('django.contrib.auth.urls')),
]
i'm expecting simple message on the browser that is 'It worked!..'
If you are using django 2.2.4 then, there is no need for using regular expression in URL pattern. change
path(r'^home/', include(('home.urls','home'), namespace='home'))
to
path('home/', include('home.urls'))
Similarly, change
url(r'^$', views.home, name='home')
to
path('', views.home, name = 'home')
For further info on URL dispatcher check https://docs.djangoproject.com/en/2.2/topics/http/urls/
Simply change this to
path('', views.home)
It will work.

django project url error

I created a project and created an application orders.
Views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'orders/index.htm')
def orders(request):
return render(request, 'orders/order.html')
urls.py:
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index.htm/', views.index, name='dashboard'),
url(r'^order.html/', views.orders, name='orders'),)
I used a ready to use site template.the link for 2 pages are:
index.htm index.htm is Dashboard
order.html order.html is Orders
when i go to link localhost:8000/orders/ , I click on orders the link looks like this.
and further click on two links in left pane multiple times, the resulting url look like this:
localhost:8000/orders/order.html/order.html
localhost:8000/orders/order.html/index.htm
I want to remove the order.html from middle so that it looks like:
localhost:8000/orders/index.htm
localhost:8000/orders/order.html
The links in your template do not have a leading slash, which means they are relative to the current directory.
If you are on the page with the URL http://localhost:8000/index.html/ and click on a hyperlink with href="order.html", it will take you to
http://localhost:8000/index.html/order.html
To fix it you must either
Add a slash to the links in your template, e.g. <a href="/order.html">
or drop the trailing slash from your URL definition in urls.py, e.g.
url(r'^index.htm', views.index, name='dashboard')
I suggest you use the template tag url in your templates instead of hard coding links.
If possible, I would also remove the .htm extension from both templates and urls.py
Change the following in urls.py file
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index/$', views.index, name='dashboard'),
url(r'^order/$', views.orders, name='orders'),)

Categories

Resources