我正在尝试为用户设置个人资料页面。它的作品时,我注销,但我需要设置的条件被重定向到登录页面,如果用户注销
网址:views.py
def login_user(req):
if req.method == 'POST':
form = AuthenticationForm(data=req.POST)
if form.is_valid():
user = form.get_user()
login(req, user)
return redirect('http://127.0.0.1:8000/')
else:
form = AuthenticationForm()
return render(req, 'loginpage.html', {'form': form})
def logout_user(req):
if req.method=='POST':
logout(req)
return redirect('home')
@login_required
def profile_page(req):
user = req.user
user_profile, created = UserProfile.objects.get_or_create(cur_user=user)
return render(req, 'profilepage.html', {'user': user, 'user_profile': user_profile})
@login_required
def edit_bio(req):
if req.method == 'POST':
new_bio = req.POST.get('bio')
user_profile = req.user.userprofile
user_profile.bio = new_bio
user_profile.save()
return redirect('profile_page')
字符串
网址:urls.py
from django.urls import path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
path('', views.homepage, name='home'),
path('signup/', views.signup, name='signup'),
path('login/', views.login_user, name='login'),
path('profile/', views.profile_page, name='profile_page'),
path('profile/edit/', views.edit_bio, name='edit_bio'),
path('logout/', views.logout_user, name='logout'),
]
urlpatterns += staticfiles_urlpatterns()
型
我该如何实现呢
2条答案
按热度按时间ygya80vv1#
此问题是How to specify the login_required redirect url in django?的重复。
解决方案是在
settings.py
中设置LOGIN_URL="/url to redirect/"
,或者将其提供给装饰器:字符串
qc6wkl3g2#
字符串
对我很有效