【问题标题】:Current path didn't match any of these当前路径与其中任何一个都不匹配
【发布时间】:2021-01-05 12:32:42
【问题描述】:

我正在尝试这样做,以便在用户注册后,它会再次重定向到登录页面,但出现错误。

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>

项目/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)

我试过制作LOGIN_REDIRECT_URL = 'accounts/login'(末尾有和没有斜线)、return HttpResponseRedirect('accounts/login/')return HttpResponseRedirect('login'),但它仍然显示错误。

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

我知道这里出了什么问题。

【问题讨论】:

  • login/ .. 最后缺少一个斜线
  • @Agawane 在return HttpResponseRedirect('accounts/login') 线上?我已经尝试在末尾添加一个斜杠,但它仍然显示错误
  • 尝试:返回 reverse('login') 而不是 HttpResponseRedirect @ConfusedCoder

标签: python django


【解决方案1】:

HttpResponseRedirect 需要 url 而不是 url-name ,所有 url 必须以 '/' 开头,因此要传递登录 url,您应该将其传递为 HttpResponseRedirect('/accounts/login/')

如 cmets 中提到的 @agawane,如果您想使用 url 本身的 url-name insted,您应该考虑使用反向 HttpResponseRedirect(reverse('login'))

  • 这是带有每个 url 名称的 django auth url file
  • 这是如何使用来自 django 文档的reverse

【讨论】:

    猜你喜欢
    • 2020-11-22
    • 2020-04-18
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多