I am new to django and I am trying to have my template be used in my project. Based on my understanding urls.py includes a url pattern variable that uses the path function. in the path function there is the url itself, view logic, and the name = variable that is used as a reference point. So when does django find out about the template? In the views.py.
So i go to views.py and it talks about the render function. Per the official tutorial and the documentation
“The render function takes the request object as its first argument, a template name as its second argument and a dictionary as its optional third argument.”
quote/urls.py
# quote/urls.py
from django.urls import path
from .views import customer_page
urlpatterns = [path("/customer", customer_page)]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# need to import views in the urls.py of quote
# Create your views here.
def customer_page(request):
return render(request, "quote/customer.html")
Error Message
Using the URLconf defined in web_scrap_cloud.urls, Django tried these URL patterns, in this order:
admin/
customer /customer
The current path, customer, didn’t match any of these.
This issue is related to your url definition and has nothing to do with your templates.
You show a segment from your quote/urls.py
file. Is this your app’s urls.py
file, or your project urls.py
file? If it is your app’s urls.py
file, please post your root urls.py
file.
Side note: The error message lists the urls that you have defined. You do not have a url “customer”.
The code snippiet above is the app urls.py.
Here is the project level urls.py
"""
URL configuration for web_scrap_cloud project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
# urls.py -> django_project url
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("customer/", include("quote.urls")),
]
per the documentation for path
path(route, view, kwargs=None, name=None)¶
My guess is, I am either misusing the include() statement or I do not have a view.
If you go to /customer//customer
you should see the result you’re expecting.
This happens because you’re including all of the urls.py
inside the quote.urls
“after” (under) the path /customer
and on that file all urls will be prefixed with /customer
In this case i would:
- Change the prefix on the root url_conf (the one with the include) to something like
quote
to create a namespace for all the urls inside the quote.urls
file:
urlpatterns = [
path("admin/", admin.site.urls),
path("quote/", include("quote.urls")),
]
- Change the url for the view on the
quote/urls.py
:
urlpatterns = [path("customer", customer_page)]
# notice how i removed the prefix slash
Now you can view your view on /quote/customer