当前路径 accounts/<int:id>/ 与其中任何一个都不匹配(对于 django 应用程序)

The current path, accounts/<int:id>/, didn’t match any of these ( for django application)

提问人:Ackeem Mclennon 提问时间:1/5/2023 更新时间:9/10/2023 访问量:163

问:

这是我试图创建的图书交换系统。我遇到的问题是尝试在登录后将用户重定向到包含个人详细信息的动态 URL。

这是我对我的项目 urs.py

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('exchange/',include('exchange.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
]

这是我对我的应用程序的 urs.py

from django.urls import path
from django.contrib.auth import views as auth_views
from . import views



urlpatterns = [
    path('', views.base, name='base'),
    path('register/', views.register_request, name='register'),
    path('accounts/<int:id>/', views.user_view, name='userview'),
    #path('login/', views.login_request, name='login'),
    path('login/', auth_views.LoginView.as_view(template_name='exchange/login.html', redirect_field_name='user_view')),

]

这是我对我的应用程序的 views.py

from django.shortcuts import render, redirect
from exchange.forms import user_login_form, user_register_form
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.contrib import messages

# Create your views here.


#Base index view
def base(request):
    return render(request, 'exchange/base.html',{})

#registration view
def register_request(request):
    if request.method=='POST':
        form = user_register_form(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = User.objects.create_user(username=username, email=email, password=password)
            user.save()
            return redirect('login')
        else:
            messages.error(request, 'Invalid form')
            render(request, 'exchange/register.html',{'form':form})
            
    else:
        form = user_register_form()
        return render(request, 'exchange/register.html',{'form':form})

#login view
def login_request(request):
    if request.method=='POST':
        form = user_login_form(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('user_view', id=user.id)
            else:
                messages.error(request, 'Invalid username or password')
                render(request, 'exchange/login.html',{'form':form})
        else:
            messages.error(request, 'Invalid form')
            render(request, 'exchange/login.html',{'form':form})
    form = user_login_form()
    return render(request, 'exchange/login.html',{'form':form})


#userview
def user_view(request, id):
    user = User.objects.get(id=id)
    return render(request, 'exchange/user_view.html',{'user':user})

我还将 LOGIN_REDIRECT_URL = '/accounts/int:id/' 添加到我的 settings.py 中。

蟒蛇 Django

评论


答:

0赞 Nealium 1/5/2023 #1

我假设您的应用程序称为exchange

因为你已将其放入 Exchange 应用程序的 URL 中,所以它实际上是在尝试匹配'accounts/<int:id>''exchange/accounts/<int:id>'

path('exchange/',include('exchange.urls')),
# Link App's Urls -> 'exchange/{x}'.format(pattern)

# exchange.urls (prepend 'exchange')
  # url: exchange
  path('', views.base, name='base'),

  # url: exchange/register
  path('register/', views.register_request, name='register'),

  # url: exchange/accounts/<int:id>
  path('accounts/<int:id>/', views.user_view, name='userview'),

  # etc

真的没有办法绕过这个预置,所以我认为你最好的选择是把它放在主 url 中,然后明确说出什么应用程序 + 视图

from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
from exchange.views import user_view

urlpatterns = [
    path('exchange/',include('exchange.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/<int:id>', user_view, name='userview'),
    path('admin/', admin.site.urls),
]
0赞 Jun 1/5/2023 #2

我没有看到你的模型,但你确定你使用user_id

<int:id>

通常我像这样使用 pk

<int:pk>
0赞 SamSparx 1/5/2023 #3

您的 URLs.py 中具有以下名称

path('accounts/<int:id>/', views.user_view, ***name='userview'***),

但是您在重定向中使用了不同的名称(带有下划线)

return redirect(***'user_view'***, id=user.id)

他们需要匹配才能使重定向正常工作

0赞 Али Паша 9/10/2023 #4

int:id/,与以下任何一个都不匹配 - 路由中不正确的字母或符号是肉眼看不见的,因此请完全手动重写路由。就我而言,也许我用另一种语言写了字母“A”