Current path didn't match any of these

Viewed 1896

I'm trying to make it so that after a user register, it will redirect to the login page again but I got an error.

Base.html:

<button class="button"><a href="{% url 'login' %}?next={{ request.path }}"> LOGIN </a></button>
    <div>
        {% if user.is_authenticated %}
        Hello, {{ user.get_username }}
        <a href="{% url 'logout' %}?next={{ request.path }}">Log Out</a>
        {% else %}
        {% if 'login' in request.path %}
        <a href="{% url 'login' %}?next={{ '/' }}">Log In</a>
        {% endif %}
        {% endif %}
    </div>

project/urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static 

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', include('drinks.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

settings.py:

LOGIN_REDIRECT_URL = '/'
STATIC_URL = '/static/'

app/urls.py:

from django.urls import path

urlpatterns = [
    path('register', registration_controller.index, name='register'),
]

registration_controller.py:

from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render
from django.conf import settings

def index(request):
    msg = ''
    if request.method == 'POST':
        req = request.POST.dict()
        username = req['username']
        password = req['password']
        email = req['email']
        try:
            user = User.objects.get(username=username)
            msg = 'Username or E-Mail is already Registered'
        except User.DoesNotExist:
            user = User.objects.create_user(username, email, password)
            user.save()
            msg = ''
            send_mail(
                'Registration Successful',
                'You are now an official member of SPLASH DRINK CORNER!',
                settings.EMAIL_HOST_USER,
                [email],
                fail_silently=True,
            )
        return HttpResponseRedirect('accounts/login')  # show login page if successful
    data = {
        'user_exists_error': msg,
    }
    return render(request, 'registration.html', data)

I've tried making the LOGIN_REDIRECT_URL = 'accounts/login' (with and without a slash at the end), return HttpResponseRedirect('accounts/login/'), return HttpResponseRedirect('login'), but it still shows an error.

The current path, home/accounts/login, didn't match any of these.

Idk what's wrong here.

1 Answers

HttpResponseRedirect requires url not url-name , all urls must start with '/' so to pass login url you should pass it as HttpResponseRedirect('/accounts/login/')

as @agawane mentiond in the comments if you want to use url-name insted of url itself you should consider using reverse HttpResponseRedirect(reverse('login'))

  • this is the django auth urls file with each url name
  • this is how to use reverse from django documintation
Related