python - 如何仅向授权用户添加评论?
问题描述
添加了添加评论的功能,使其只有授权用户才能添加评论,但由于某种原因这不起作用,请修复它。
我还添加了标签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
写什么文件还需要显示,我会显示谢谢大家!
解决方案
<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>
推荐阅读
- mysql - 如何从节点js中的mysql db中找出给定时间的时区
- python - 必须使用 Subscription 实例作为第一个参数调用未绑定方法 delete() (Stripe API)
- flutter - 如何在 onComplete 函数后更改小部件本身的参数
- python - 使用 For Each 循环从列表中删除项目的 Python 逻辑错误
- git - 文件的 Git 权限
- asp.net - Asp.net 核心 web api 应用程序,startup.cs 文件中发生错误
- javascript - TypeError: (0 , _index.default) 在使用 Babel 作为 Rollup 包中的库时不是函数
- python - Plesk + Python Flask - 在第二个网站上配置代理不起作用
- firebase - 如何在 2 个 vue-native 应用程序之间进行通信
- java - 从 OPC 服务器循环收集数据