Harry Whitnear
Harry Whitnear

Reputation: 189

Django form text field not displaying

I have used django's models and forms to add a comment section to a blog app, however the form's text field will not show when loaded in the in the browser. Only the submit button loads with no textfield to submit with.

models.py

from django.db import models
from datetime import datetime
from django import forms

class Post(models.Model):
    title = models.CharField(max_length=140)
    body = models.TextField()
    date = models.DateTimeField("date published", default=datetime.now())

    def _str_(self):
        return self.title

class Comment(models.Model):
    post = models.ForeignKey('blog.post', on_delete=models.CASCADE, related_name='comments')
    author = models.CharField(max_length=100)
    text = models.TextField(max_length=200)
    created_date = models.DateTimeField(default=datetime.now())

    def _str_ (self):
        return self.text

forms.py

from django import forms
from django.forms import ModelForm
from .models import Comment
from django.db import models

class CommentForm(forms.ModelForm):
    class Meta:
        models = Comment
        exclude = ['post']

form = CommentForm

post.html

<div>
    <div class="container">
        <h5>New comment</h5>

        <form method="POST">
            {% csrf_token %}
            {{ form.as_p }}
            <br>
            <button class="btn" style="background-color:lightblue" type="submit">Submit Comment</button>
        </form>
    </div>
</div>

views.py

from django.shortcuts import render
from django.views import generic
from .forms import CommentForm
from .models import Comment

def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('/blog/<int:pk>', pk=post.pk)
    else:
        form = CommentForm()
        return render(request, 'blog/post.html', {"form":form})
    form = CommentForm(request.POST)

form = CommentForm()

In django's admin i can add new comments and the form loads as expected there but does not work on the blog post itself. Admin submitted comments through the admin panel are also saving and displaying fine.

Upvotes: 1

Views: 1614

Answers (1)

danleyb2
danleyb2

Reputation: 1078

you have a typo in forms.py

change models to model

from django import forms
from django.forms import ModelForm
from .models import Comment
from django.db import models

class CommentForm(forms.ModelForm):
   class Meta:
       model = Comment # changed from models
       exclude = ['post']

in views.py

you are missing imports

from django.shortcuts import get_object_or_404,redirect
from .models import Post

other changes
in your models.py
the to string method for classes is __str__ not _str_

with this changes i was able to reproduce this django project with your snippets that works in posting comments

Upvotes: 1

Related Questions