这里是 Django 新手。我正在处理一个包含几个应用程序的项目,因此将我的模板保留在我的项目级别。现在,这里的问题是在相应的 url 上没有检测到一些模板。例如,与属性列表相对应的模板(property_list.html)在相关的 url(/properties)被检测到,而 property_detail.html 和 property_new.html 分别对应于 properties/new 和 properties/[insert property ID] 。仅作记录,“主页”、“注册”、“登录”工作正常。
我在这里和其他地方都查找过类似的实例,但似乎没有任何东西指向我想要的方向。那么,是什么给了?
模板结构的屏幕截图在下面的链接图片中。同样,该文件夹位于根/项目级别。
模板结构
项目设置(模板部分)
# ...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# ...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
# ...
项目网址
from django.contrib import admin
from django.urls import path, include
from .views import HomePageView
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('agencies/', include('agencies.urls')),
path('properties/', include('properties.urls')),
]
属性网址
from django.urls import path
from . import views
urlpatterns = [
path('', views.PropertyListView.as_view(), name='property_list'),
path('new/', views.PropertyCreateView.as_view(), name='property_new'),
path('<slug:pk>/', views.PropertyDetailView.as_view(), name='property_detail'),
]
属性 View
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from .models import Property
# Create your views here.
class PropertyListView(ListView):
model = Property
template_name = 'property_list.html'
class PropertyDetailView(DetailView):
model = Property
template_name = 'property_detail.html'
class PropertyCreateView(LoginRequiredMixin, CreateView):
model = Property
template_name = 'property_new.html'
fields = [
'property_type',
'is_for_sale',
'cost',
'location',
'num_of_bedrooms',
'num_of_bathrooms',
'num_of_parking_spaces',
'num_of_garages',
'has_pool',
'has_waterfront',
'has_elevator',
'added_on',
]
login_url = 'login'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
# class PropertyUpdateView(LoginRequiredMixin, UpdateView):
# model = Property
# fields = ['title', 'body', ]
# template_name = 'property_edit.html'
# login_url = 'login'
# class PropertyDeleteView(LoginRequiredMixin, DeleteView):
# model = Property
# template_name = 'property_delete.html'
# success_url = reverse_lazy('property_list')
# login_url = 'login'
#
请您参考如下方法:
因为在你的设置中你有 'DIRS': [(os.path.join(BASE_DIR, 'templates'))] 那么默认情况下 django 在名为“templates”的目录下查找文件名“在你的根上。
在您的模板目录结构中,您添加了另一个名为 properties 的目录,其中包含 property_list.html 因此,为了让 view 找到模板必须指定起始 template 目录的相对路径。
template_name = 'properties/property_list.html'
template_name = 'properties/property_new.html'
享受吧。
