问题
I'm using modal for login/signup in django.
(http://bootsnipp.com/snippets/featured/modal-login-with-jquery-effects)
But I wonder how django treat this in template rendering.
As you can see above url, it has only one html file, say, login.html.
In django views, I have two views named LoginView and SignupView.
These are what I thought as problems.
Each
Viewgonna rendertemplate:LoginView-->login.htmlSignupView-->signup.html
but as you can see above, if I used modal, there is only one html file for both signup and login. I want to know how I can deal with it.
- Also, Each
viewgonna pass itsform, say,UserCreationForm,CustomLoginFormfor getting input from user. But if there were only onehtmlfile, would there be conflict between thoseforms?
Thanks.
回答1:
1) You can move both of them into one view that returns both of the forms, with different names.
signup_form = UserCreationForm()
login_form = CustomLoginForm()
context = {"signup_form": signup_form, "login_form": login_form, **other_kwargs}
return render(request, context, content_type=...)
2) There's no problem with a page that contains several forms. Since the forms are being rendered in different names, one idea is to distinguish which form gets posted, using a hidden input, so basically in your html, you do something like:
<form method="POST" action="URL">
input type="hidden" name="form_name" value="login_form"
{{ login_form.as_table }}
input type="submit"
</form>
<form method="POST" action="URL">
input type="hidden" name="form_name" value="signup_form"
{{ signup_form.as_table }}
input type="submit"
</form>
You can also point URL to a single view, handle which form was submitted using the form_name key inside the POST parameters. Or you can have them point to other URLs, send ajax requests or any other way you prefer.
来源:https://stackoverflow.com/questions/39091589/how-does-django-treat-login-signup-based-on-modal