Why does LOGIN_REDIRECT_URL not work

Hi guys,

I have added the LoginRequiredMixin to some of my views, for example this one:

it is the shortest, that’s why it’s this example :slight_smile:

class LanguageWordListView(LoginRequiredMixin,ListView):
    '''users will pick a language later on and get a filtered list of elements which they can click'''
    template_name = 'templates/word_list.html'
    model = Word
    def get_queryset(self):
        language = self.kwargs['language'].lower()
        return Word.objects.filter(language__name__iexact= language)

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['words'] = self.get_queryset()
        context['language'] = self.kwargs['language'].lower()
        return context

in project_name/project_name/settings.py i have specified LOGIN_REDIRECT_URL = ‘/’

and it worked before I added the LoginRequiredMixin I could login and it redirected to ‘/’ which it does not do anymore. Not even after removing the Mixin again, just for testing…
It logs in, though and let’s me do everything that I did before, but I cannot get the redirect to work again.
I changed one more thing: I added {% extends “VocabTrainer/templates/base.html” %} to my login.html

and now I receive in dev-tools a 403 upon clicking on login:

for reference, my file structuer is here:

can anyone explain what’s going on?

Yes, this 403 is likely telling you that there’s something wrong with the login view that you are posting to, or the data that you are posting to it - it’s not an issue with your redirect. (You may still have an issue with the redirect, but this message isn’t necessarily directly related to that.)

You’ll want to look at your server log to get more details about what’s happening on the server. (For example, if you’re using runserver, look at the console log where you’re running runserver.)

from my runserver terminal I only get the following lines:

[07/Dec/2023 16:03:11] "GET /accounts/login/ HTTP/1.1" 200 1745
[07/Dec/2023 16:03:18] "POST /accounts/login/ HTTP/1.1" 302 0
[07/Dec/2023 16:03:18] "GET / HTTP/1.1" 200 2477

as opposed to the following without base.html

[07/Dec/2023 16:04:26] "POST /accounts/login/ HTTP/1.1" 302 0
[07/Dec/2023 16:04:26] "GET / HTTP/1.1" 200 2477
[07/Dec/2023 16:04:26] "GET /static/css/style.css HTTP/1.1" 304 0

this doesn’t help me, though :frowning:

edit I didn’t specify a login-view, neither did I specify a login-form, I am just using the built-in stuff at the moment.

I just tried to add the extension to the logged_out.html, where it works!

as this is really weird and the edits are getting longer and annoying to follow:

when I comment the JQuery out of the base.html it works:

this does not work:

<head>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
    <link rel="stylesheet" href="{%  static 'css/style.css' %}">

    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <meta charset="UTF-8">
    <title>Word Up!</title>
</head>

while this

<head>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
    <link rel="stylesheet" href="{%  static 'css/style.css' %}">

{#    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>#}
    <meta charset="UTF-8">
    <title>Word Up!</title>
</head>

works.

That much is apparently evident from the trace back in your console error.

The next steps would then be to look at the Ajax request being issued in your main.js

and, I keep saying this: your comments are usually helpful and to the point. You are awesome.
I went bug-hunting and found that I was using an overly generic jquery $(‘form’).submit() inside of my ajax-scripts. After rewriting a tiny bit, from $('form') to $('#word-form') the forwarding works.
So i used the html-element “form” and now I am using an ID to select only the form that I actually need.