Django simple forum project not displaying modles on page

Question:

I have been working on a simple forums application for a few weeks now. The main goal of the project is to have sooner written a post and then for the post to be displayed. The project does not utilize user models. For some reason when the user completes the form for their post their post is not displayed. I was wondering if anyone knew why this is happening or if any of y’all have any tips.

views.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from . import forms
# Create your views here.

class ForumForm(generic.CreateView):
    template_name = 'forums_simple/forum.html'
    form_class = forms.ForumForm
    success_url = '/'

    def form_vaild(self, form):
        self.object = form.save(commit=False)
        self.object.save()
        return super().form_vaild(form)

urls.py

from django.contrib import admin
from django.urls import path, include
from . import views

app_name = 'forums'

urlpatterns = [
    path('', views.ForumForm.as_view(), name='forum')
]

models.py

from django.db import models

# Create your models here.
class Post(models.Model):
    message = models.TextField(blank=True, null=False)
    created_at = models.DateTimeField(auto_now=True)

forms.py

from django import forms
from  . import models

class ForumForm(forms.ModelForm):
    class Meta:
        model = models.Post
        fields = ('message',)

forum.html

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}
<div class="container">
  {% for message in object_list %}
    {{ post.message }}
  {% endfor %}
</div>
<div class="container">
  <form class="forum_class" action="index.html" method="post">
    {% csrf_token %}
    {% crispy form %}
    <button type="submit" name="big-button"></button>
  </form>
</div>
{% endblock %}
Asked By: user17083547

||

Answers:

You need to add object_list to your context in the view so:

 def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # don't forget to import Post
        context['object_list'] = Post.objects.all()
        return context
Answered By: Super sub
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.