为什么当我单击其他用户时,我又回到当前的请求用户?

问题描述

对不起,我不得不截取一些屏幕截图以解释发生了什么事情。现在,我的个人资料很多,有许多不同的用户,当我使用其中一个登录时,我会假设用户名是:medoabdin就像您现在在屏幕截图中找到的(medoabdin),对我来说它的名称是(Origin)是当前的用户请求。因此,现在我也有由不同用户创建的许多不同问题,并且当我想输入任何其他配置文件时,让我们假设该用户是当前请求用户(abdelhamedab​​din),它使我返回到当前请求(medoabdin),而不返回我回到阿卜杜勒·哈马达卜。

但是,当我检查链接URL时,我发现链接是正确的,并且当我与(abdelhamedab​​din)用户登录时,我看到针对(medoabdin)发生了同样的事情,所以,谁能告诉我发生了什么事情?

这些是屏幕截图:

current request (medoabdin)

several questions

show the link url for different users

accounts / 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 %}
                            {% if user.first_name != '' and user.last_name != '' %}
                                <h4>{{ user.first_name }} {{ user.last_name }}</h4>
                            {% else %}
                                <h4>User Profile</h4>
                            {% endif %}
                        </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 %}

帐户/views.py

@method_decorator(login_required,name='dispatch')
# view profile page
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):
        self.request.session['switch_comment'] = False
        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)

community / views.py

# List all questions + search
class UserQuestions(ListView):
    template_name = 'community/user_questions.html'
    context_object_name = 'all_objects'
    queryset = UserAsking

    def get_context_data(self,object_list=queryset,**kwargs):
        context = super().get_context_data(**kwargs)
        # paginator
        context['all_objects'] = UserAsking.objects.all()
        paginator = Paginator(context['all_objects'],5)
        page_number = self.request.GET.get('page_number')
        context['all_objects'] = paginator.get_page(page_number)
        # search
        context['query'] = self.request.GET.get("query",'')
        if context['query']:
            all_objects = UserAsking.objects.all().order_by('-date')
            context['all_objects'] = all_objects.filter(
                Q(title__contains=self.request.GET['query']) |
                Q(question__contains=self.request.GET['query']) |
                Q(field__contains=self.request.GET['query'])
            )
        return context

社区/user_questions.py

{% extends 'base.html' %}
{% block title %} All Questions That People Asked {% endblock %}

{% block body %}
{% if request.user.is_authenticated %}
    <div class="all-questions">
        <div class="container">
            <div class="fl-left hidden-sm hidden-xs">
                <h2>All Questions</h2>
            </div>
            <div class="fl-right hidden-sm hidden-xs">
                <a href="{% url 'community:user_asking' %}" class="btn btn-primary btn-lg">Ask Question</a>
            </div>
            <div class="clear"></div>
            <div class="row">
                <div class="add-q">
                    <div class="col-sm-12 visible-sm-block visible-xs-block">
                        <h2>All Questions</h2>
                    </div>
                    <div class="col-sm-12 visible-sm-block visible-xs-block">
                        <a href="{% url 'community:user_asking' %}" class="btn btn-primary btn-lg">Ask Question</a>
                    </div>
                </div>
            {% if all_objects %}
                <div class="col-sm-12">
                    <div class="questions">
                        {% for post in all_objects %}
                            <div class="q_section">
                                <a class="text-primary title" href="{% url 'community:question_view' post.ask_slug %}">{{ post.title }}</a>
                                <p class="field">{{ post.field }}</p>
                                <div class="info fl-right">
                                    <span class="time">{{ post.date }}</span> |
                                    <a href="{% url 'accounts:view_profile' post.userprofile.slug %}" style="font-size:14px">

                                        {% if post.userprofile.user.first_name != '' %}
                                            {{ post.userprofile.user.first_name }}
                                        {% else %}
                                            User
                                        {% endif %}

                                        <img class="logo-image" style="width:25px;height: 25px" src="
                                        {% if request.user.userprofile.logo %}
                                            {{ request.user.userprofile.logo.url }}
                                        {% else %}
                                            {% load static %}
                                            {% static 'index/images/default-logo.jpg' %}
                                        {% endif %}
                                        ">
                                    </a>
                                </div>
                                <div class="">
                                    <!--a class="btn btn-primary btn-lg" href="{# {% url 'community:delete_post' post.id %} #}">
                                        <i class="fa fa-trash x2"></i>
                                    </a-->
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                </div>
                {% else %}
                <h2 class="text-center text-info">No Questions</h2>
                {% endif %}
            </div>
            <!-- Pagination -->
            {% if all_objects %}
            <div class="pagination">
                <span class="step-links">
                    {% if all_objects.has_prevIoUs %}
                        <a href="?page_number=1">&laquo; first</a>
                        <a href="?page_number={{ all_objects.prevIoUs_page_number }}">prevIoUs</a>
                    {% endif %}

                    <span class="current">
                        Page {{ all_objects.number }} of {{ all_objects.paginator.num_pages }}.
                    </span>

                    {% if all_objects.has_next %}
                        <a href="?page_number={{ all_objects.next_page_number }}">next</a>
                        <a href="?page_number={{ all_objects.paginator.num_pages }}">last &raquo;</a>
                    {% endif %}
                </span>
            </div>
            {% endif %}
        </div>
    </div>

    {% include 'base_footer.html' %}

{% endif %}
{% endblock %}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)