如何在同一页面上显示两个表单?我会感谢你的帮助
这是我的views.py:
def register_user(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password=form.cleaned_data['password'])
user.save()
user_profil = UserProfil(user=user, name=form.cleaned_data['name'], last_name=form.cleaned_data['last_name'],city=form.cleaned_data['city'])
user_profil.save()
return HttpResponseRedirect('/profile/')
return render_to_response('access.html', {'formRegister':form}, context_instance=RequestContext(request))
else:
form = RegistrationForm()
return render_to_response('access.html', {'formRegister':form}, context_instance=RequestContext(request))
def login_user(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = LoginForm(request.POST)
if forml.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user_profil = authenticate(username= username, password=password)
if user_profil is not None:
login(request, user_profil)
return HttpResponseRedirect('/profile/')
else:
return HttpResponseRedirect('/login/')
return render_to_response('access.html', {'formLogin':form}, context_instance=RequestContext(request))
else:
form = LoginForm()
context = {'formLogin':form}
return render_to_response('access.html', context, context_instance=RequestContext(request))
这是我的 urls.py
url(r'^access/$','users.views.register_user'),
url(r'^access/$','users.views.login_user'),
访问.html:
<form action="" method="post">{%csrf_token%}
{{ formRegister.as_p }}
<p><input type="submit" value="Rejestruj"/></p>
</form>
<hr>
<form action="" method="post">{%csrf_token%}
{{ formLogin.as_p }}
<input type="submit" value="Login"/>
</form>
有一个模板(access.html)和两个视图
当我替换 urls.py(order) 时:
url(r'^access/$','users.views.login_user'),
url(r'^access/$','users.views.register_user'),
仅显示登录表单