我有一个允许用户创建个人资料并登录的应用程序。
当用户登录时,他被重定向到 127.0.0.1:8000/profile/
问题是,我想通过将用户的用户名添加到 URL 的末尾来自定义 URL,例如 127.0.0.1:8000/profile/michael
这是一个与我类似的问题
Django - 登录后,将用户重定向到他的自定义页面 --> mysite.com/username
“获取用户名,然后对自定义 URL 执行 HttpResponseRedirect。”
我似乎无法弄清楚如何将用户名作为参数传递给 HttpResponseRedirect 以正确处理到自定义 URL。
return HttpResponseRedirect('/profile/?username=%s' % (username, request.path))
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
person = Person.objects.get(user=request.user)
return render(request,'profile.html',{'person':person})
我的网址
url(
r'^profile/$',
'pet.views.Profile',
name = 'Profile'
),
新的
我的意见.py
def LoginRequest(request):
if request.user.is_authenticated():
username = User.objects.get(username=request.user)
url = reverse('Profile', kwargs = {'username': username.username})
return HttpResponseRedirect(url)
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
Person = authenticate(username=username, password=password)
if Person is not None:
login(request, Person)
username= User.objects.get(username=request.user)
url = reverse('Profile', kwargs = {'username': username.username})
return HttpResponseRedirect(url)
return render(request, 'login.html',{'form': LoginForm()})
url(
r'^login/$',
'pet.views.LoginRequest',
name = 'LoginRequest'
),
url(
r'^profile/(?P<username>\w+)/$',
'pet.views.Profile',
name = 'Profile'
),
def Profile(request,username):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user__username=username)
return render(request,'profile.html',{'board':board ,'person':person})