I am trying to create a CreateView in Django, but am facing a 'template not found' error (shown below)
Currently, I have the following code:
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"),
]
You can see I have already used a ListView and DetailView which has worked fine. The code from these views also extends 'event_blog/base.html' so I do not expect this to be the issue.
Here is the directory for the files:
And the HTML code for the template if you needed it:
{% 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 %}
Also the model:
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
Perhaps the default template name has changed, but I couldn't find any information to state this. I have also tried adding the following line to my view:
template_name = "event_blog/eventpost_form.html"
However, this also proved unsuccessful. Is there something I am doing wrong?
Thanks to @raphael in the comments, turns out I was missing the file extension on my template file... 🤦♀️
'eventpost_form' should have been 'eventpost_form.html'