python - 无法从 Django 中的给定位置加载模板
问题描述
我无法使用以下网址加载 product_create.html
http://127.0.0.1:8000/create/
正如您在标题Template-loader postmortem下的最后一行中看到的那样,它正在我的模板所在的以下位置搜索模板(检查项目布局)。
C:\trydjango\products\templates\products\product_create.html
以下是我的项目布局
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',
],
},
},
]
网址.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),
]
产品/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)
解决方案
推荐阅读
- r - 使用 | 链接 filter(across()) 语句 (OR) 代替 & (AND)
- javascript - Yeoman 生成器添加新文件生成现有项目
- javascript - 如何在“观察者”中跟踪某些对象的变化?
- python - SqlAlchemy 在 engine.dispose() 之后重新创建池
- python - 熊猫中的Groupby返回太多行
- python - SQLalchemy 建立类之间的关系
- c++ - 给定带有指向键的非常量类的指针的 std::map,如何通过指向 const 的指针访问它?
- python - 如何在 python 中使用多处理计算 f1 分数?
- c# - 创建自定义 OAuth2 服务器
- reactjs - React - 谓词功能组件