簡體   English   中英

當前路徑與其中任何一個都不匹配

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

我正在嘗試做到這一點,以便在用戶注冊后,它將再次重定向到登錄頁面,但出現錯誤。

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)

設置.py:

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

應用程序/urls.py:

from django.urls import path

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

注冊控制器.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.

我知道這里有什么問題。

HttpResponseRedirect 需要 url 而不是 url-name,所有 url 必須以 '/' 開頭,因此要通過登錄 url 您應該將其傳遞為HttpResponseRedirect('/accounts/login/')

正如評論中提到的@agawane ,如果您想使用 url 本身的 url-name insted,您應該考慮使用反向HttpResponseRedirect(reverse('login'))

  • 這是 django auth urls 文件,每個 url 名稱
  • 這是從 django 文檔中使用反向的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM