I am unable to load product_create.html using following url
http://127.0.0.1:8000/create/
Following is the error
As you can see in the last line under heading Template-loader postmortem, it is searching for template in following location where my template is present (check project layout).
C:rydjangoproductsemplatesproductsproduct_create.html
Following is my project layout
.
Relevant part of settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR),"templates"],
#'DIRS': [path.joinpath(BASE_DIR, "templates")],
#'DIRS': [BASE_DIR / 'templates' ],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
urls.py
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view,product_create_view
#its better fom the alternate version
#from pages import views and then using views.home_view
urlpatterns = [
path('', home_view ,name = 'home'),
#the 1st argument to path gives the url
path('admin', admin.site.urls),
path('contact/', contact_view ,name = 'contact'),
path('about/', about_view ,name = 'about'),
path('product/', product_detail_view),
path('create/', product_create_view),
]
products/views.py
from django.shortcuts import render
from .forms import ProductForm
from .models import Product
# Create your views here.
def product_create_view(request):
form = ProductForm(request.POST or None)
if form.is_valid():
form.save()
context={
'form':form
}
return render(request,"products/product_create.html",context)
def product_detail_view(request):
obj=Product.objects.get(id=1)
# context={
# 'title':obj.title,
# 'description':obj.description
# }
context={
'object':obj
}
return render(request,"products/product_detail.html",context)