這是view.py如何在沒有django中的確認框的情況下刷新表單有效後的登錄頁面?
def login_user(request):
form = LoginForm(request.POST or None)
if request.method == '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:
if user.is_active:
login(request, user)
# Redirect to a success page.
userprofile = UserProfile.objects.get(user = user)
request.session['userprofile'] = userprofile
request.session['familymembers'] = UserProfile.objects.filter(family = userprofile.family)
return redirect('/profile')
else:
# Return a 'disabled account' error message
messages.add_message(request, messages.ERROR, '.')
return redirect('/profile')
else:
messages.add_message(request, messages.ERROR, '.')
return render_to_response('registration/login.html',{'form':form},RequestContext(request))
return render_to_response('registration/login.html', {'form': form},RequestContext(request))
elif request.method == 'GET':
return render_to_response('registration/login.html',{'form':form},context_instance = RequestContext(request))
我登錄時FUNC形式is_valid()
失敗,同樣的登錄頁面顯示錯誤消息, 現在,當我刷新頁面,一個確認框彈出說,提交表單確認,
我的問題是:有可能避免此確認框,我試圖
return redirect('/', {'form': form},RequestContext(request))
這將避免確認框,但有效的錯誤消息已不存在。
請看[django網站](https://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view)中的例子,並告訴if它有幫助與否。 – scriptmonster 2013-02-21 09:24:46