首页 > 解决方案 > 如何仅向授权用户添加评论?

问题描述

添加了添加评论的功能,使其只有授权用户才能添加评论,但由于某种原因这不起作用,请修复它。

我还添加了标签strong,但由于某种原因它也不起作用

post_detail.html

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

{% block content %}
    <link href="{% static 'css/post_detail.css' %}" rel="stylesheet">
    <div class="post-entry">
        <h2>{{ post.title }}</h2>
        <p>{{ post.body|urlize }}</p>
    </div>

    <p><a href="{% url 'post_edit' post.pk %}">+ Edit Blog Post</a></p>
    <p><a href="{% url 'post_delete' post.pk %}">+ Delete Blog Post</a></p>
    {% if post.header_image %}
        <p><img src="{{post.header_image.url}}"></p>
    {% else %}
        <p></p>
    {% endif %}


    {% for comm in post.commentpost_set.all%}
        {{ comm.user }} <br>
        {{ comm.text }} <br><br>
    {% endfor %}

    <br>
    <hr>
    <h2>Comments...</h2>

    {% if not post.comments.all %}
        No Comments Yet...<a href="{% url 'post_comment' post.pk %}">
        Add Comment</a>
    {% else %}

    <form method="post">
        {% csrf_token %}
        {{ comment_form.as_p }}
        {% if request.user.is_authenticated %}
            <a href="{% url 'post_comment' post.pk %}">Add Comment</a><br><br>
        {% else %}
            <a href="{% url 'post_comment' post.pk %}">Add Comment</a><br><br disabled>
        {% endif %}
    </form>

        {% for comment in post.comments.all %}
            <strong>
                {{ comment.name }} -
                {{ comment.date_added }}
            </strong>
            <br>
            {{ comment.body }}
            <br><br>

        {% endfor %}
    {% endif %}



{% endblock content %}

视图.py

from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy, reverse
from .models import Post, Comment
from .forms import CommentForm
from django.http import HttpResponseRedirect


class BlogListView(ListView):
    model = Post
    template_name = 'home.html'

    context_object_name = 'posts'
    paginate_by = 2
    queryset = Post.objects.all()

class BlogDetailView(DetailView):
    model = Post
    template_name = 'post_detail.html'

class BlogCreateView(CreateView):
    model = Post
    template_name = 'post_new.html'
    fields = ['title', 'author', 'body', 'header_image']

class BlogCommentView(CreateView):
    model = Comment
    form_class = CommentForm
    template_name = 'post_comment.html'
    def form_valid(self, form):
        form.instance.post_id = self.kwargs['pk']
        return super().form_valid(form)

    success_url = reverse_lazy('home')
    #fields = '__all__'

class BlogUpdateView(UpdateView):
    model = Post
    template_name = 'post_edit.html'
    fields = ['title', 'body', 'header_image']

class BlogDeleteView(DeleteView):
    model = Post
    template_name = 'post_delete.html'
    success_url = reverse_lazy('home')



@property
def image_url(self):
    if self.image:
        return getattr(self.photo, 'url', None)
    return None

写什么文件还需要显示,我会显示谢谢大家!

标签: pythondjangodjango-comments

解决方案


<form method="post">
        {% csrf_token %}
        {{ comment_form.as_p }}
        {% if request.user.is_authenticated %}
            <a href="{% url 'post_comment' post.pk %}">Add Comment</a><br><br>
        {% else %}
            <a href="">Add Comment</a><br><br>
        {% endif %}
</form>

推荐阅读