Django博客评论作者参考

问题描述 投票:0回答:1

我建立了一个小型的Django Blog应用,其中有一个评论区。用户在发表文章或评论之前需要登录。我想返回 全名 我的 自定义用户 在我的模板中的Blog文章和评论中。在Blog文章后,全名如期返回,但评论的作者返回的是 用户名栏 我的 自定义用户 模型即 . 请帮助我。这是我的代码。

CustomUser模型

from django.contrib.auth.models import AbstractUser
from django.db import models
from . manager import CustomUserManager

class CustomUser(AbstractUser):
    email           = models.EmailField(verbose_name = 'email address', unique=True)
    username        = None
    first_name      = models.CharField(verbose_name = 'first_name', blank = True, max_length = 30)
    last_name       = models.CharField(verbose_name = 'last_name', blank = True, max_length = 30)
    bio             = models.CharField(max_length = 150, null = True, blank = True)
    avatar          = models.ImageField(upload_to = 'avatars', null = True, blank = True)
    date_joined     = models.DateTimeField(auto_now_add = True)
    last_login      = models.DateTimeField(auto_now_add = True)     

    def __str__(self):
        return self.first_name + " " + self.last_name

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

博客模式

from django.conf import settings
from django.db import models
from django.urls import reverse
from django.contrib.auth import get_user_model



class Post(models.Model):
    title = models.CharField(max_length=200)
    slug   = models.SlugField(max_length = 200, unique = True)
    body = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name = 'blog_posts',
        )
    post_picture = models.ImageField(upload_to = 'post_picture', null = True, blank = True)
    approved_comment = models.BooleanField(default=True)

    def approve(self):
        self.approved_comment = True
        self.save()

    class Meta:
        ordering = ['created_on']

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post_detail', args=[str(self.id)])

    def approved_comments(self):
        return self.comments.filter(approved_comment = True)

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    author = models.ForeignKey(
                settings.AUTH_USER_MODEL,
                on_delete=models.CASCADE,
                related_name = 'blog_comments',
                null = True,
    )
    text = models.CharField(max_length = 300,)
    created_date = models.DateTimeField(auto_now_add = True)
    approved_comment = models.BooleanField(default=True)

    def approve(self):
        self.approved_comment = True
        self.save()

    def __str__(self):
        return self.text

查看

from django.views.generic import ListView, DetailView, CreateView
from django.views.generic.edit import UpdateView, DeleteView, FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from . models import Post, Comment
from .forms import PostForm, CommentForm
from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth.decorators import login_required

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    template_name = 'post_new.html'
    fields = ['title', 'body',]
    login_url = 'login'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

class PostListView(ListView):
    model = Post
    template_name = 'post_list.html'

    def get_queryset(self):
        return Post.objects.order_by('created_on').reverse()


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

class PostUpdateView(LoginRequiredMixin, UpdateView):
    model = Post
    fields = ['title', 'body', ]
    template_name = 'post_edit.html'
    login_url = 'login'

class PostDeleteView(LoginRequiredMixin, DeleteView):
    model = Post
    template_name = 'post_delete.html'
    success_url = reverse_lazy('post_list')
    login_url = 'login'

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

@login_required
def comment_approve(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.approve()
    return redirect('post_detail', pk=comment.post.pk)

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

模板

{% extends 'blog_template.html' %}
{% load static %}
{% load widget_tweaks %}


{% block blog_center %}
    <div class="post-entry">
        <h2>{{ object.title }}</h2>
        <p>by {{ object.author }} | {{ object.date }}</p>
        <p>{{ object.body }}</p>
    </div>
        <p>Back to <a href="{% url 'post_list' %}">All Posts</a>.</p>

    {% if user.is_authenticated %}
        <a class="" href="{% url 'add_comment_to_post' pk=post.pk %}">Add comment</a>  
    {% else %}
        <a href="{% url 'login' %}">Log In</a>
    {% endif %}

    {% for comment in post.comments.all %}
        {% if user.is_authenticated or comment.approved_comment %}
            <div class="">
                <div class="">
                    {{ comment.created_date }}
                    {% if not comment.approved_comment %}

                        <a class="btn btn-default" href="{% url 'comment_remove' pk=comment.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
                        <a class="btn btn-default" href="{% url 'comment_approve' pk=comment.pk %}"><span class="glyphicon glyphicon-ok"></span></a>
                    {% endif %}            
                </div>
                <p>{{ comment.author }}</p>
                <p>{{ comment.text|linebreaks }}</p>
            </div>
        {% endif %}       
    {% empty %}
            <p>No comments here yet :</p>
    {% endfor %}
{% endblock blog_center %}
comments blogs django-custom-user
1个回答
0
投票

我不知道是什么原因造成的,但我用另一种方法实现了,这很有效。

我首先在我的评论模型中创建了一个CharField。

然后,我在表单验证后,在视图中为该字段分配了 request.user。然后我把它保存到数据库中。

然后我在我的模板中返回值。这是我更新的代码。

models.py

from django.conf import settings
from django.db import models
from django.urls import reverse
from django.contrib.auth import get_user_model


class Post(models.Model):
    title           = models.CharField(max_length=200)
    slug            = models.SlugField(max_length = 200, unique = True)
    body            = models.TextField()
    created_on      = models.DateTimeField(auto_now_add=True)
    author          = models.ForeignKey(
                        settings.AUTH_USER_MODEL,
                        on_delete=models.CASCADE,
                        related_name = 'blog_posts',
                        )
    post_picture    = models.ImageField(
                        upload_to = 'post_picture', 
                        null = True, 
                        blank = True,
                        )
    approved_comment = models.BooleanField(default=True)

    def approve(self):
        self.approved_comment = True
        self.save()

    class Meta:
        ordering = ['created_on']

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post_detail', args=[str(self.id)])

    def approved_comments(self):
        return self.comments.filter(approved_comment = True)

class Comment(models.Model):
    post                    = models.ForeignKey(
                                Post, 
                                on_delete=models.CASCADE, 
                                related_name='comments'
                                )
    comment_author          = models.CharField(max_length = 200, null = True)
    text                    = models.CharField(max_length = 300,)
    created_date            = models.DateTimeField(auto_now_add = True)
    approved_comment        = models.BooleanField(default=True)

    def approve(self):
        self.approved_comment = True
        self.save()

    def __str__(self):
        return self.text

views.py

from django.views.generic import ListView, DetailView, CreateView
from django.views.generic.edit import UpdateView, DeleteView, FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from . models import Post, Comment
from .forms import PostForm, CommentForm
from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth.decorators import login_required

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    template_name = 'post_new.html'
    fields = ['title', 'body',]
    login_url = 'login'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

class PostListView(ListView):
    model = Post
    template_name = 'post_list.html'

    def get_queryset(self):
        return Post.objects.order_by('created_on').reverse()


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

class PostUpdateView(LoginRequiredMixin, UpdateView):
    model = Post
    fields = ['title', 'body', ]
    template_name = 'post_edit.html'
    login_url = 'login'

class PostDeleteView(LoginRequiredMixin, DeleteView):
    model = Post
    template_name = 'post_delete.html'
    success_url = reverse_lazy('post_list')
    login_url = 'login'

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

@login_required
def comment_approve(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.approve()
    return redirect('post_detail', pk=comment.post.pk)

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

模板

{% extends 'blog_template.html' %}
{% load static %}
{% load widget_tweaks %}


{% block blog_center %}
    <div class="post-entry">
        <h2>{{ object.title }}</h2>
        <p>by {{ object.author }} | {{ object.created_on }}</p>
        <p>{{ object.body }}</p>
    </div>
        <p>Back to <a href="{% url 'post_list' %}">All Posts</a>.</p>

    {% if user.is_authenticated %}
        <a class="" href="{% url 'add_comment_to_post' pk=post.pk %}">Add comment</a>  
    {% else %}
        <a href="{% url 'login' %}">Log In</a>
    {% endif %}

    {% for comment in post.comments.all %}
        {% if user.is_authenticated or comment.approved_comment %}
            <div class="">
                <div class="">
                    {{ comment.created_date }}
                    {% if not comment.approved_comment %}

                        <a class="btn btn-default" href="{% url 'comment_remove' pk=comment.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
                        <a class="btn btn-default" href="{% url 'comment_approve' pk=comment.pk %}"><span class="glyphicon glyphicon-ok"></span></a>
                    {% endif %}            
                </div>
                {{ comment.comment_author }} 
                {{ comment.text|linebreaks }}
            </div>
        {% endif %}       
    {% empty %}
            <p>No comments here yet :</p>
    {% endfor %}
{% endblock blog_center %}
© www.soinside.com 2019 - 2024. All rights reserved.