Django 不保存评论

问题描述

我正在建立一个论坛,用户可以在其中发布内容并对其发表评论。我创建了评论部分按钮,一切正常。但是,我无法保存我的评论,在我点击“添加评论”后,它根本不显示

这是我的models.py

class Comment(models.Model): 
    post = models.ForeignKey(BlogPost,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80) 
    email = models.EmailField() 
    body = models.TextField() 
    created = models.DateTimeField(auto_Now_add=True) 
    updated = models.DateTimeField(auto_Now=True) 
    active = models.BooleanField(default=True) 

    class Meta: 
        ordering = ('created',) 

    def __str__(self): 
        return 'Comment by {} on {}'.format(self.name,self.post) 

admin.py

from .models import BlogPost,Comment

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('name','email','post','created','active')
    list_filter = ('active','updated')
    search_fields = ('name','body')

forms.py

from .models import Comment

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('name','body')

views.py

from .models import BlogPost,Comment
from .forms import EmailPostForm,CommentForm

def post_detail(request,year,month,day,post):
    post = get_object_or_404(BlogPost,slug=post,status='published',publish__year=year,publish__month=month,publish__day=day)

    # List of active comments for this post
    comments = post.comments.filter(active=True)

    new_comment = None

    if request.method == 'POST':
        # A comment was posted
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            # Create Comment object but don't save to database yet          
            new_comment = comment_form.save(commit=False)
            # Assign the current post to the comment
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        comment_form = CommentForm()                   
    return render(request,'blog/post/detail.html',{'post': post,'comments': comments,'new_comment': new_comment,'comment_form': comment_form})

这是detail_blog.html

{% extends 'base.html' %}
{% load static %}

{% block content %}


<style type="text/css">
    .card{
        max-width: 700px;
    }
    .card-body{
        padding: 20px;
    }
</style>

    <div class="row">
        <div id="-particles-js"></div>
        <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
        <script> particlesJS.load('particles-js','particles.json',function(){ console.log('particles.json loaded...'); }); </script>
        
        <!-- Blog Post -->
        <div class="card m-auto">
            {% if blog_post.image %}
            <img class="card-img-top" src="{{blog_post.image.url}}">
            {% endif %}
            <div class="card-body mt-2 mb-2">
              <h2 class="card-title">{{blog_post.title}}</h2>
              <p class="card-text">{{blog_post.body|safe}}</p>

              {% if blog_post.author == request.user %}
                <a href="{% url 'blog:edit' blog_post.slug %}" class="btn btn-primary">Update</a>
              {% endif %}
            </div>
            {% for comment in comments %}
            <div class="form-group">
              <p class="info">
                Comment {{ forloop.counter }} by {{ comment.name }}
                    {{ comment.created }}
                  </p>
                  {{ comment.body|linebreaks }}
                    </div>
              {% empty %}
                <p>There are no comments yet.</p>
          {% endfor %}

          {% if new_comment %}
                <h2>Your comment has been added.</h2>
          {% else %}
            <form class="create-form" method="post" enctype="multipart/form-data">
              {{ comment_form.as_p }}
              {% csrf_token %}
              <textarea class="form-control" rows="10" type="text" name="body" id="id_body" placeholder="Your comment..." required></textarea>
              <p ><input type="submit" value="Add comment" class="btn btn-primary"></p>
            </form>
          {% endif %}
        
            <div class="card-footer text-muted">
              Updated on {{blog_post.date_updated}} by {{blog_post.author}}
            </div>
        </div>
    </div>
</div>

    

{% endblock content %}

虽然我在正确的设置中得到了一切,但它不起作用。我无法对帖子

enter image description here

发表评论

这是我的文件在我的目录中的位置:

enter image description here

任何指导将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)