django 未找到View template

w8biq8rn  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(131)

我试图在Django中创建一个模板视图,但遇到了一个“template not found”错误(如下所示)


的数据
目前,我有以下代码:
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"


然而,这也证明是不成功的。我做错了什么吗?

xzv2uavs

xzv2uavs1#

感谢@raphael在评论中,原来我在我的模板文件上丢失了文件扩展名。🤦‍
“eventpost_form”应该是“eventpost_form. html”

相关问题