django 3.1 multilanguage site breaks login

I have set up a django 3.1 site that uses 2 languages, English and German, and I use i18n_patterns in my urls.py. The problem is, in my views.py method, I never get a POST request, even though I specify method=”POST” in my form. It always comes in as a GET request, and I cannot login. Somewhere along the way, the system changes my POST request into a GET request.

urlpatterns = []
urlpatterns += i18n_patterns(
    path('{}/'.format(settings.DJANGO_ADMIN_URL), admin.site.urls),
    path('review/', include('review.urls')),
    path('anmelden_success/', views.anmelden_success, name='anmelden_success'),
    path('', views.site_login, name='site_login'),
    path('site_login/', views.site_login, name='site_login'),
    path('site_logout/', views.site_logout, name='site_logout'),
)

I log in with this form, specifying method=”POST”:

<form action="/site_login/" method="POST" id="login-form">{% csrf_token %}

<label class="required" for="id_username">Benutzer:</label> <input type="text" name="username" autofocus required id="id_username">

<label class="required" for="id_password">Passwort:</label>&nbsp;<input type="password" name="password" required id="id_password">
<input type="hidden" name="next" value="/review/">  

<label>&nbsp;</label><input type="submit" value="Anmelden">

</form>

The request comes to my views.py method site_login:

def site_login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('/anmelden_success')
        else:
            return HttpResponse("Sorry, you failed to login.")
    else:
        if request.user.is_authenticated:
            return redirect('/review')
        else:
            return render(request, 'registration/login.html')

Since it somehow gets changed from a POST request to a GET request, my login fails, and I come straight back to my login page with the /de/ (or /en/) prepended: /de/site_login

If I remove i18n_patterns from my urls.py, I can login using the POST request.

urlpatterns = [
    path('{}/'.format(settings.DJANGO_ADMIN_URL), admin.site.urls),
    path('review/', include('review.urls')),
    path('anmelden_success/', views.anmelden_success, name='anmelden_success'),
    path('', views.site_login, name='site_login'),
    path('site_login/', views.site_login, name='site_login'),
    path('site_logout/', views.site_logout, name='site_logout'),

]
#urlpatterns += i18n_patterns()

With this urls.py config, I can login. But I have lost the multiple language functionality.

How can I have both?

Thanks in advance for any tips.

Answer

try to add url like this <form action="{% url 'site_login' %}"> in your form instead of <form action="/site_login/">