使用详细信息视图查看Django中的用户程序



我在简单的博客网站上需要帮助。用户可以写帖子的地方。每个帖子作者都必须签名才能写一篇文章。但是读者不必签名。

我正在使用Django详细信息,以允许读者查看邮政作者的配置文件。

个人资料视图将有作家的详细信息以及作者所写的其他帖子。

我使用以下逻辑进行视图https://chriskief.com/2012/12/29/django-generic-detailview-without-a-pk-or-slug/

以下是细节:

所有这些都在我的网站中的帐户应用中。以下代码被打破并给出错误

错误消息的图像

帐户/views.py:

    class ProfileView(DetailView):
            model = User
            template_name = 'accounts/profile.html'
            def get_user_profile(self, username):   
                return get_object_or_404(User, pk=username)
     #I know pk=username is not correct. I am not sure what to put pk=?
  # I was able to get the writers other posts using the code below. I did not have to show this code for this question. But just to show you that the pk above has to be username. Or Else the code below won't work(I guess)        
            def get_context_data(self, **kwargs):
                context = super(ProfileView, self).get_context_data(**kwargs)
                context['post_list'] = Post.objects.filter(user__username__iexact=self.kwargs.get('username'))
                return context  

我的模型如下

accounts/models.py:

from django.contrib.auth.models import User
from django.db import models
    class UserProfile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        city = models.CharField(max_length=100)
        country = models.CharField(max_length=100)
        bio = models.TextField(default='', blank=True)
        profile_image = models.ImageField(default='', blank=True, null='')

        def __str__(self):
            return "Profile of {}".format(self.user.username)

下面是我的模板

    {% extends 'base.html' %}
    {% block body %}
    <div class="content">
        <h1>{{user.first_name}} {{user.last_name}}</h1>
        <p>            
            <br/>From: City, Country
            <br/>Bio
            <br/>Profile Image
        </p>
    </div>

    {% endblock %}

帐户/urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth.views import LoginView, LogoutView
app_name = 'accounts'
urlpatterns = [
    url(r'^login/$', LoginView.as_view(template_name='accounts/login.html'), name='login'),
    url(r'^logout/$', LogoutView.as_view(), name='logout'),
    url(r'^signup/$', views.SignUp.as_view(), name='signup'),
    url(r'^profile/(?P<username>[-w]+)/$', views.ProfileView.as_view(), name='profile'),
]

错误消息文本

atteributeError at/eaccept/profile/3/必须使用对象PK或slug调用通用详细信息概要文件。请求方法:获取请求URL:http://127.0.0.1:8000/accounts/profile/3/Django版本:1.11异常类型:attributeError异常值:
必须使用对象PK或slug调用通用详细信息概要文件。异常位置:c: user samir miniconda3 envs mydjangoenv lib lib site-packages django-1.11-py3.6.egg django django django views generit generic generic generic genter.py in get_object,get_object,第49行49python可执行文件:c: users samir miniconda3 envs mydjangoenv python.exePython版本:3.6.4Python路径:.........

如果您使用的是'用户名'字段来获取用户,则可能需要将过滤器更改为使用"用户名"字段,因为它是唯一的:

尝试更改功能:

def get_user_profile(self, username):   
    return get_object_or_404(User, pk=username)

to

def get_object(self):
    return get_object_or_404(User, username=self.kwargs.get('username'))

如果您希望对象为用户填充,请稍微将其更改为

def get_object(self):
        return get_object_or_404(UserProfile, user__username=self.kwargs['username'])

相关内容

最新更新