在Django制作Twitter克隆,在显示tweets时无法显示正确的用户

2024-04-28 09:46:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有用户注册。每当我以某个用户的身份登录时,所有的tweet都会被称为该用户的tweet,即使它不是

表单.py

from django.contrib.auth.models import User
from django import forms

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

class Meta:
    model = User
    fields = ['username', 'password', 'email']

型号.py

from django.db import models
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.contrib.auth.models import User

class Howl(models.Model):
    author = models.ForeignKey('auth.user', null=True)
    content = models.CharField(max_length=150)
    published_date = models.DateTimeField(default=timezone.now)
    like_count = models.IntegerField(default=0)
    rehowl_count = models.IntegerField(default=0)

    def get_absolute_url(self):
        return reverse('howl:index')

    def __str__(self):
        return self.content 

索引.html

{% block content %}
<h2>Index.html timeline!</h2>
{% for howl in howls %}
    <div class="howl">
        <h2><strong>{{user.username}}</strong></h2>
        <p class="lead">{{howl.content}} - {{howl.published_date}}</p>
        <span>Rehowls: {{howl.rehowl_count}}, Likes: {{howl.like_count}}</span>
    </div><!--howl-->
{% endfor%}
{% endblock %}

视图.py

from django.shortcuts import render, redirect
from django.views import generic
from django.views.generic import View 
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Howl
from .forms import UserForm
from django.contrib.auth import authenticate, login

class IndexView(generic.ListView):
    template_name = 'howl/index.html'
    context_object_name = 'howls'

    def get_queryset(self):
        return Howl.objects.all()

class HowlCreate(CreateView):
    model = Howl
    fields = ['content']

class UserFormView(View):
    form_class = UserForm
    template_name = 'howl/registration_form.html'

    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():
            user = form.save(commit=False)

            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user.set_password(password)
            user.save()

            user = authenticate(username=username, password=password)

            if user is not None:

                if user.is_active:
                    login(request, user)
                    return redirect('howl:index')

我知道使用用户名在索引.html这就是问题所在。它显示了作为这些tweet作者登录的当前用户。如何使其显示tweet的合法所有者?你知道吗


Tags: djangofromimportselfformreturnmodelsdef
2条回答

在我的HowlCreate视图中,我没有将作者设置为howl。我只设置内容。你知道吗

class HowlCreate(CreateView):
    model = Howl
    fields = ['content']

    # This sets up the author 
    def form_valid(self, form):
        instance = form.save(commit=False)
        instance.author = self.request.user # set current user as author
        instance.save()

        return HttpResponseRedirect(self.get_success_url())

在模板中,显示的是登录用户{{user.username}},而不是{{howl.author.username}}

相关问题 更多 >