B
B
bimka2021-11-21 17:59:15
Django
bimka, 2021-11-21 17:59:15

Why doesn't it redirect to the login page?

I'm using LoginRequiredMixin - not logged in users should be redirected to login page, but it doesn't happen.

mysite/autos/views.py:

from django.views import View
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, UpdateView, DeleteView


from autos.models import Auto, Make

class MainView(View, LoginRequiredMixin):
    login_url = '/login/'
    redirect_field_name = 'next'

    def get(self, request):
        mc = Make.objects.all().count()
        al = Auto.objects.all()
        ctx = {'make_count': mc, 'auto_list': al }
        return render(request, 'autos/autos_list.html', ctx)

class MakeList(View, LoginRequiredMixin):
    def get(self, request):
        make_list = Make.objects.all()
        ctx = {'make_list': make_list}
        return render(request, 'autos/make_list.html', ctx)
...


mysite/autos/urls.py:
from django.urls import path

from . import views

app_name = 'autos'

urlpatterns = [
    path('', views.MainView.as_view(), name = 'all'),
    path('make_create/', views.MakeCreate.as_view(), name = 'make_create'),
    path('make_list/', views.MakeList.as_view(), name = 'make_list'),
    path('auto_create/', views.AutoCreate.as_view(), name = 'auto_create'),
    path('<int:pk>/update/', views.AutoUpdate.as_view(), name = 'auto_update'),
    path('<int:pk>/delete/', views.AutoDelete.as_view(), name = 'auto_delete'),
    path('make_list/<int:pk>/update/', views.MakeUpdate.as_view(), name = 'make_update'),
    path('make_list/<int:pk>/delete/', views.MakeDelete.as_view(), name = 'make_delete'),
]


mysite/autos/templates/autos/autos_list.html:
{% extends "base_bootstrap.html" %}

{% block content %}
<h1>Список автомобилей</h1>
{% if user.is_authenticated %}
    {% if auto_list %}
        <ul>
            {% for auto in auto_list %}
                <li>
                    {{ auto.nickname }} ({{ auto.make }})
                    <a href="{% url 'autos:auto_update' auto.id %}">Обновить информацию</a> |
                    <a href="{% url 'autos:auto_delete' auto.id %}">Удалить</a></br>
                    Комментарий: {{ auto.comments }} (пробег: {{ auto.mileage }})
                </li>
            {% endfor %}

        </ul>
    {% else %}
        <p>Пока в базе данных машин нет</p>
    {% endif %}
{% else %}
    <p>Только авторизованные пользователи могут просмаатриватьь эту страницу</p>
{% endif %}

{% endblock %}


mysite/home/templates/registration/login.html:
{% extends "base_bootstrap.html" %}

{% block content %}

{% if form.errors %}
  <p>Your username and password didn't match. Please try again.</p>
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bimka, 2021-11-29
@bimka

I solved the issue like this
in views.py in the class names, I swapped the parent classes so that LoginRequiredMixin went first.
It was:

class MainView(View, LoginRequiredMixin):
    login_url = '/login/'
    redirect_field_name = 'next'

    def get(self, request):
        mc = Make.objects.all().count()
        al = Auto.objects.all()
        ctx = {'make_count': mc, 'auto_list': al }
        return render(request, 'autos/autos_list.html', ctx)

class MakeList(View, LoginRequiredMixin):
    def get(self, request):
        make_list = Make.objects.all()
        ctx = {'make_list': make_list}
        return render(request, 'autos/make_list.html', ctx)

became:
class MainView(LoginRequiredMixin, View):
    login_url = '/login/'
    redirect_field_name = 'next'

    def get(self, request):
        mc = Make.objects.all().count()
        al = Auto.objects.all()
        ctx = {'make_count': mc, 'auto_list': al }
        return render(request, 'autos/autos_list.html', ctx)

class MakeList(LoginRequiredMixin, View):
    def get(self, request):
        make_list = Make.objects.all()
        ctx = {'make_list': make_list}
        return render(request, 'autos/make_list.html', ctx)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question