且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

django如何根据模态处理登录/注册?

更新时间:2023-12-02 09:33:16

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)包含多个表单的页面没有问题。由于表单以不同的名称呈现,一个想法是使用隐藏的输入区分哪个表单被发布,所以基本上在你的html中,你做了类似的事情:

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>

您还可以将URL指向单个视图,使用其中的form_name键处理提交的表单POST参数。或者您可以让他们指向其他网址,发送ajax请求或您喜欢的任何其他方式。

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.