简体   繁体   English

当前路径与其中任何一个都不匹配

[英]Current path didn't match any of these

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: 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:项目/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:设置.py:

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

app/urls.py:应用程序/urls.py:

from django.urls import path

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

registration_controller.py:注册控制器.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.我试过制作LOGIN_REDIRECT_URL = 'accounts/login' (最后有和没有斜杠), return HttpResponseRedirect('accounts/login/')return HttpResponseRedirect('login') ,但它仍然显示错误。

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

Idk what's wrong here.我知道这里有什么问题。

HttpResponseRedirect requires url not url-name, all urls must start with '/' so to pass login url you should pass it as HttpResponseRedirect('/accounts/login/') HttpResponseRedirect 需要 url 而不是 url-name,所有 url 必须以 '/' 开头,因此要通过登录 url 您应该将其传递为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'))正如评论中提到的@agawane ,如果您想使用 url 本身的 url-name insted,您应该考虑使用反向HttpResponseRedirect(reverse('login'))

  • this is the django auth urls file with each url name这是 django auth urls 文件,每个 url 名称
  • this is how to usereverse from django documintation这是从 django 文档中使用反向的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM