注册后,我检查user_type并将用户移动到三个网页之一。
显示正确的页面布局,但网址与注册页面保持一致。
EG:url说" http://127.0.0.1:8000/users/register/" 页面显示:"用户类型1信息"
我认为使用重定向应该更改网址,但它似乎无法正常工作。
views.py:
arrayTopGainStock[i] = [stockName, stockAOM_VALUE, stockAOM_VOLUMN];
urls.py
def register(request):
if request.method == 'POST':
form = RegisterForm(data=request.POST)
if form.is_valid():
# profile = form.save(commit=False)
# profile.user = request.user
# profile.save()
user = form.save()
profile = user.userprofile
user_group = form.cleaned_data.get('user_type')
profile.user_type = user_group
profile.save()
# form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user_group = form.cleaned_data.get('user_type')
user = authenticate(username = username, password = password)
login(request, user)
print(user_group)
print(type(user_group))
if user_group == '1':
return HttpResponseRedirect("/users/business/")
# return
# HttpResponseRedirect('http://127.0.0.1:8000/users/business/')
# return HttpResponseRedirect(reverse('users:business'))
# return redirect(request, 'users/business.html')
elif user_group == '2':
return redirect(request, 'users/student.html')
elif user_group == '3':
return redirect(request, 'users/tourist.html')
# else:
# return redirect('main:index')
else:
print(RegisterForm.errors)
else:
form = RegisterForm()
def business_view(request):
# return redirect(request, 'users/business.html')
# return HttpResponseRedirect('users/business.html')
# return redirect('/users/business/')
return redirect('users/business')
def student_view(request):
return redirect(request, 'users/student.html')
def tourist_view(request):
return redirect(request, 'users/tourist.html')
感谢您的帮助。
答案 0 :(得分:0)
仅在重定向参数中使用 urlname ,即在学生页面上重定向使用return redirect('student')
(定义网址时定义的名称)。例如
if user_group == '2':
return redirect('student')
同样在您要显示html页面的视图中,请改用render
。例如。
from django.shorcuts import render
def student_view(request):
return render(request, 'users/student.html')