This is a book exchange system I'm trying to create. The challlenge I'm having is trying to redirect the user to a dynamic url consisting of there personal details after log in.
这是我试图创建的一个图书交换系统。我面临的挑战是试图在登录后将用户重定向到包含三个个人详细信息的动态URL。
Here is my urs.py for my project
这是我的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),
]
Here is my urs.py for my app
这是我的应用程序的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')),
]
Here is my views.py for my app
以下是我的应用程序的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})
I have also added LOGIN_REDIRECT_URL = '/accounts/int:id/' to my settings.py.
我还将LOGIN_REDIRECT_URL=‘/ACCOUNTS/INT:ID/’添加到我的settings.py中。
更多回答
优秀答案推荐
I presume your app is called exchange
我假设您的应用程序名为Exchange
because you've put this 'accounts/<int:id>'
inside your exchange app's urls, it's actually trying to match for 'exchange/accounts/<int:id>'
因为您已经在您的Exchange应用程序的URL中放入了这一项‘ACCOUNTS/
’,所以它实际上是在尝试匹配‘Exchange/ACCOUNTS/
’
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
There's really no way around this prepending, so i think your best bet would be to put it in the main urls and just explicitly say what app + view
确实没有办法绕过这个前置,所以我认为你最好的办法是把它放在主URL中,然后明确地说出APP+的视图
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),
]
I do not see your models but are you sure that you use user_id like
我没有看到您的模型,但您确定您使用的用户ID如下
<int:id>
Normally I use pk like this
通常我是这样使用PK的
<int:pk>
You have the following name in your URLs.py
您的URLs.py中包含以下名称
path('accounts/<int:id>/', views.user_view, ***name='userview'***),
But you are using a different name (with an underscore) in your redirect
但您在重定向中使用了不同的名称(带下划线
return redirect(***'user_view'***, id=user.id)
They will need to match for the redirect to work
它们需要匹配才能使重定向起作用
int:id/, didn’t match any of these - An incorrect letter or symbol in the route is invisible to the eye, so rewrite the route completely manually. in my case, perhaps I wrote the letter "A" in another language
INT:ID/,与其中任何一个都不匹配--路线中的错误字母或符号是肉眼看不见的,因此完全手动重写路线。在我的例子中,也许我用另一种语言写了字母“A”
更多回答
我是一名优秀的程序员,十分优秀!