【问题标题】:how can I access userprofile from another user?如何从其他用户访问用户配置文件?
【发布时间】:2020-11-28 10:10:24
【问题描述】:

当我相应地创建用户和用户配置文件时,我需要一些帮助,当我尝试访问其他用户的任何用户时,请求将我打开我正在处理的请求,而不是真正的用户,虽然,我正在使用slug 指定我点击它的用户可能我无法准确解释发生在我身上的事情以获得更多解释,请点击该视频以显示我的意思:https://www.youtube.com/watch?v=MzSo0ay2_Xk&feature=youtu.be

账户应用

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.template.defaultfilters import slugify

CHOICE = [('male', 'male'), ('female', 'female')]


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    slug = models.SlugField(max_length=100, unique=True, blank=True)
    overview = models.TextField(editable=True, blank=True, default='You have no an Overview yet')
    city = models.CharField(max_length=20, blank=False)
    phone = models.IntegerField(default=0, blank=True)
    sex = models.CharField(max_length=10, default='male', choices=CHOICE)
    skill = models.CharField(max_length=100, default='You have no skills yet')
    logo = models.ImageField(upload_to='images/', default='images/default-logo.jpg', blank=True)

    def __str__(self):
        return self.user.username

    def save(self, *args, **kwargs):
        self.slug = slugify(self.user)
        super().save(*args, **kwargs)


def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = UserProfile.objects.create(user=kwargs['instance'])


post_save.connect(receiver=create_profile, sender=User)

view.py

class ViewProfile(UpdateView):
    queryset = UserProfile.objects.all()
    template_name = 'accounts/profile.html'
    form_class = UpdateInfoForm
    slug_field = 'slug'
    slug_url_kwarg = 'user_slug'

    def get_success_url(self):
        return reverse_lazy('accounts:view_profile', kwargs={'user_slug': self.request.user.userprofile.slug})

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        user_prof = UserProfile.objects.get(user=self.request.user)
        context['user_prof'] = user_prof
        context['get_favourite'] = User.objects.get(username=self.request.user.username).favorite.all()
        return context

    def form_valid(self, form):
        form.instance.user_slug = self.request.user.userprofile.slug
        self.object = form.save()
        return super().form_valid(form)

profile.html

{% extends 'base.html' %}
{% block title %} {{ user.first_name }} {{ user.last_name }} Profile {% endblock %}

{% block body %}
    <!-- User Profile Section -->
{% if user.is_authenticated %}
<div class="profile">
    <div class="container-fluid">
        <div class="col-md-1">
            <div class="thumbnail">
                <div class="row">
                    <div class="col-xs-12">
                        <!-- Profile View Section -->
                        <div class="logo-image text-center">
                            {% if user.userprofile.logo %}
                            <div class="my-image">
                                {% if request.user.username == user.userprofile.slug %}
                                <a href="{% url 'accounts:user_image' user.userprofile.slug %}">
                                    <img class="img-responsive" src="{{ user.userprofile.logo.url }}">
                                </a>
                                <span>
                                    <a href="{% url 'accounts:add_avatar' user.userprofile.slug %}" class="fa fa-camera fa-1x text-center">
                                        <p>Upload Image</p>
                                    </a>
                                </span>
                                {% endif %}
                            </div>

                            {% else %}

                            {% load static %}
                            <div class="my-image">
                                <img class="img-responsive img-thumbnail" src="{% static 'index/images/default-logo.jpg' %}">
                                <span>
                                    <a href="{% url 'accounts:add_avatar' user.userprofile.slug %}" class="fa fa-camera fa-1x text-center">
                                        <p>Upload Image</p>
                                    </a>
                                </span>
                            </div>
                            {% endif %}
                            <h4>{{ user.first_name }} {{ user.last_name }}</h4>
                        </div>
                    </div>

                    <div class="col-xs-12">
                        <div class="caption">
                        <ul class="nav nav-pills nav-stacked">
                            <li role="presentation" class="active"><a href="#overview" class="trigger" data-target="#overview">Overview</a></li>
                            <li role="presentation" class=""><a href="#personal-information" class="trigger" data-target="#personal-information">Personal Information</a></li>
                            <li role="presentation" class=""><a href="#my-skills" class="trigger" data-target="#my-skills">Skills</a></li>
                        </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Information Sections -->
        <div class="col-md-8 col-md-offset-3 information">
            <div class="overview show" id="overview">
                <h2 class="line">Overview</h2>
                <p class="lead">{{ user.userprofile.overview }}</p>
                <a data-placement="bottom" title="update overview"  class="fa fa-edit" data-toggle="modal" data-tooltip="tooltip" data-target=".overview_info"></a>
            </div>
            <div class="personal-info" id="personal-information">
                <h2 class="line">Personal Information</h2>
                <p class="lead">City: {{ user.userprofile.city }}</p>
                <p class="lead">Phone Number: 0{{ user.userprofile.phone }}</p>
                <p class="lead">Sex: {{ user.userprofile.sex }}</p>
                <a data-placement="bottom" title="update personal information"  class="fa fa-edit" data-toggle="modal" data-tooltip="tooltip" data-target=".personal_info"></a>
            </div>
            <div class="skill" id="my-skills">
                <h2 class="line">Skills:</h2>
                <p class="lead">{{ user.userprofile.skill }}</p>
                <a data-placement="bottom" title="update skills" class="fa fa-edit" data-toggle="modal" data-tooltip="tooltip" data-target=".skills"></a>
            </div>
        </div>
        <!-- get all questions -->
        {% if user_prof.userasking_set.all %}
        <div class="col-md-8 col-md-offset-3 user_questions">
            <h2 class="line">All Questions You Asked</h2>
            {% for questions in user_prof.userasking_set.all %}
                <p><a href="{% url 'community:question_view' questions.ask_slug %}">{{ questions.title }}</a></p>
            {% endfor %}
        </div>
        {% endif %}
        <!-- get favourites -->
        {% if get_favourite %}
        <div class="col-md-8 col-md-offset-3 user_questions">
            <h2 class="line">Favourites</h2>
            {% for fav in get_favourite %}
                <p><a href="{% url 'community:question_view' fav.ask_slug %}">{{ fav.title }}</a></p>
            {% endfor %}
        </div>
        {% endif %}
    </div>

{% include 'accounts/information_form.html' %}

</div>
    {% include 'base_footer.html' %}
{% endif %}
{% endblock %}

【问题讨论】:

    标签: django authentication user-permissions


    【解决方案1】:

    self.request.user 指的是当前登录的用户。这就是您为所有用户获得相同结果的原因。

    要得到你想要的,你需要在你的视图中做如下改变:

        user_prof = UserProfile.objects.get(pk=self.kwargs['pk']) 
    

    而且,我不知道您为什么要使用UpdateView,而您只是想显示 对象?你应该改用TemplateView

    【讨论】:

    • 但我不能将 self 放入 user_prof 因为该方法不在类中!我正在使用 UpdateView,因为我需要编辑配置文件中的某些字段
    • 您应该有单独的视图来编辑和显示对象。
    • 您的意思是视图错误是因为我必须将视图从更新更改为模板,并且我必须将每个视图与其他视图分开吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2014-10-26
    • 1970-01-01
    • 2017-04-25
    • 2018-06-06
    • 1970-01-01
    相关资源
    最近更新 更多