How does django treat login/signup based on modal?

别说谁变了你拦得住时间么 提交于 2019-12-11 00:49:44

问题


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.

  1. Each View gonna render template :

    • LoginView --> login.html
    • SignupView --> 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.

  1. Also, Each view gonna pass its form, say, UserCreationForm, CustomLoginForm for getting input from user. But if there were only one html file, would there be conflict between those forms?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!