提问人:Kiana 提问时间:11/11/2023 更新时间:11/11/2023 访问量:33
未找到 CreateView 模板
CreateView template not found
问:
我正在尝试在 Django 中创建一个 CreateView,但遇到“找不到模板”错误(如下所示)
目前,我有以下代码:
views.py:
from django.shortcuts import render
from django.views.generic import (
ListView, DetailView, CreateView, DeleteView
)
from .models import EventPost
class EventPostListView(ListView):
model = EventPost
template_name = "event_blog/home.html"
context_object_name = "event_posts"
ordering = ['-date_posted']
class EventPostDetailView(DetailView):
model = EventPost
class EventPostCreateView(CreateView):
model = EventPost
fields = ['name', 'location', 'locked', 'description', 'image']
urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path("", EventPostListView.as_view(), name="event-blog-home"),
path("event/<int:pk>/", EventPostDetailView.as_view(), name="post-detail"),
path("event/new/", EventPostCreateView.as_view(), name="post-create"),
]
您可以看到我已经使用了 ListView 和 DetailView,它们运行良好。这些视图中的代码还扩展了“event_blog/base.html”,因此我不认为这是问题所在。
以下是文件的目录:
如果需要,还有模板的 HTML 代码:
{% extends "event_blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div>
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New Event</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-secondary mt-3 mb-3" type="submit">Post</button>
</div>
</form>
</div>
{% endblock content %}
还有模型:
class EventPost(models.Model):
name = models.CharField(max_length=100)
location = models.CharField(max_length=100)
locked = models.BooleanField(default=True, editable=True)
description = models.TextField()
attendance = models.IntegerField(default=0)
image = models.ImageField(upload_to='event_pics', blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
也许默认模板名称已更改,但我找不到任何信息来说明这一点。我还尝试将以下行添加到我的视图中:
template_name = "event_blog/eventpost_form.html"
然而,这也被证明是不成功的。我做错了什么吗?
答:
0赞
Kiana
11/11/2023
#1
多亏了评论中的@raphael,原来我的模板文件缺少文件扩展名......🤦♀️
“eventpost_form”应该是“eventpost_form.html”
评论