A
A
Andrei Ramanchyk2017-04-29 23:52:39
Django
Andrei Ramanchyk, 2017-04-29 23:52:39

Why is relogin happening? (it is necessary to implement limited access if the logged in user does not match the one selected in the drop-down list)?

1. Based on the list of performers from the auth_user table, I build an html page

{% for us in users %}
        {{ forloop.counter }}
        <tr>
            <th>{{ us.id }}</th>
            <th><a href={% url 'projects' pk=us.id name=us.username %}> {{ us.username }}</a></th>
            <th>{{ user.groups }}</th>
        </tr>
    {% endfor %}

2. I select one of the users from the table and follow the link
in urls.py
url(r'^projects/(?P<name>\w+)-(?P<pk>\d+)/$', ProjectsView.as_view(), name='projects'),

3. in views.py
class ProjectsView(DetailView):
    model = User
    template_name = 'projects.html'

    def get_context_data(self, **kwargs):
        context = super(ProjectsView, self).get_context_data(**kwargs)
        modules = ModuleWork.objects.filter(executor=self.kwargs['pk'])
        context['name'] = self.kwargs['name']
        context['modules'] = modules
        return context

4. I pass all this to the template
5. before that, I logged in with one of the users of the table from point 1
and want to make sure that if I log in and choose not my name in point 2, I could not edit the data, but only viewed, for example .
{% block table %}
    <div class="scrollable">
        <table>
            <thead>
            <tr>
                <th>#</th>
                <th>Код проекта</th>
                <th>Исполнитель проекта</th>
                <th>Описание проекта</th>
                <th>Дата проекта</th>
            </tr>
            </thead>
            <tbody>
            {% for module in modules %}
                <tr>
                <th>{{ module.pk }}</th>
                <th><a href={% url 'module' pk=module.id%}>{{ module.project }}-{{ module.room }}{{ module.room_number }}-
                {{ module.module }}{{ module.module_number }}</a></th>
                <th>{{ module.executor }}</th>
                <th>{{ module.project }}</th>
                <th>{{ module.module_work_is_produced }}</th>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

{% endblock %}

Now when I click on the name of item 2, on the next page I have a re-login automatically.
0dc6dcd1df554e60abb8c9fdbed72c15.PNG14caa6a161054ae3940eba6f0288bd58.PNG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrei Ramanchyk, 2017-05-02
@jagrmi

javedimka @javedimka
Andrei Ramanchyk: One Google query said - apparently, DetailView returns two variables to the context through which you can access the object - object and the model name itself in lowercase, i.e. user, so your user is overwritten in the template.
Well, about request.user - in the new jungs it turns out that just user can be, as in your code, BUT since there is such a problem with names, direct access through request should solve the problem, and then - request.user.is_authenticated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question