Django博客添加评论文本字段未显示

时间:2019-12-29 11:11:31

标签: python django

我已经使用django的模型和表单在博客应用程序中添加了评论部分,但是当我尝试在帖子中添加评论时,在浏览器中加载表单时,表单的文本字段将不会显示。仅显示提交按钮和标题,没有文本字段可供提交。

models.py

from django.db import models
from django.utils import timezone


# Create your models here.
class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    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=200)
    created_date = models.DateTimeField(default=timezone.now)
    text = models.TextField()

    def __str__(self):
        return self.text

forms.py

from django import forms
from .models import Post, Comment


class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'text')


class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('author', 'text',)

views.py

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_list_or_404, get_object_or_404, redirect
from django.utils import timezone

from blog.forms import PostForm
from .models import Post, Comment
from .forms import PostForm, CommentForm


# Create your views here.
def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
    stuff_for_frontend = {'posts': posts}
    return render(request, 'blog/post_list.html', stuff_for_frontend)


def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    stuff_for_frontend = {'post': post}
    return render(request, 'blog/post_detail.html', stuff_for_frontend)


@login_required
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('post_detail', pk=post.pk)

    else:

        form = PostForm()
        stuff_for_frontend = {'form': form}
    return render(request, 'blog/post_edit.html', stuff_for_frontend)


def post_edit(request, pk):
    post = get_list_or_404(Post, pk=pk)
    if request.method == 'POST':

        # updating existing form
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('post_detail', pk=post.pk)
        else:
            form = PostForm(instance=post)
            stuff_for_frontend = {'form': form, 'post': post}
        return render(request, 'blog/post_edit.html', stuff_for_frontend)


@login_required
def post_draft_list(request):
    posts = Post.objects.filter(published_date__isnull=True).order_by('-created_date')
    stuff_for_frontend = {'posts': posts}
    return render(request, 'blog/post_drafts_list.html', stuff_for_frontend)


@login_required
def post_publish(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.publish()
    return redirect('post_detail', pk=pk)


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.author = request.user
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog/add_comment_to_post.html', {form: 'form'})


def comment_remove(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.delete()
    return redirect('post_detail', pk=comment.post.pk)

添加评论HTML

{% extends 'blog/base.html' %}
{% block title %} Add Comment {% endblock %}
{% block content %}
<h1>New Comment</h1>
<form method="POST" class="post-form-blog">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="save btn btn-default">Post</button>
</form>
{% endblock %}

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您只是在这一行中混了两个项目:

return render(request, 'blog/add_comment_to_post.html', {form: 'form'})

应该是

return render(request, 'blog/add_comment_to_post.html', {'form': form})