why when I click a different user returns me back to the current request user?

120 Views Asked by At

sorry, I had to take some screenshots to explains what is going on with me. now, I have much profile that has many different users and when I log in with one of it I'll assume the username is: medoabdin like you find on the screenshot now (medoabdin) and the name of it is (Origin) for me is a current user request. so, now I have also many different questions created by the different users, and when I want to enter any other profile let's suppose the user is (abdelhamedabdin) by current request user it returns me back to the current request (medoabdin) and not returns me back to abdelhamedabdin.

however, when I check the link URL I find the link is correct and when I log in with (abdelhamedabdin) user I see the same thing occurs to me against (medoabdin) so, can anyone tell me what is going on guys?

these are screenshots:

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 %}

accounts/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

community/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 %}
0

There are 0 best solutions below