Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

python - Unable to load template from a given location in Django

I am unable to load product_create.html using following url

http://127.0.0.1:8000/create/

Following is the error enter image description here

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

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...