没有动作的 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/641292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:26:22  来源:igfitidea点击:

HTML Forms without actions

htmldjangoformsdjango-forms

提问by interstar

In Django / Pinax, I've come across the login form which starts like this :

在 Django / Pinax 中,我遇到过这样开头的登录表单:

<form class="login" method="POST" action="">

It works perfectly well. So I assume that either some java-script or something in the Django framework is putting the value into the action attribute.
So, my questions:

它工作得很好。因此,我假设某些 java 脚本或 Django 框架中的某些内容将值放入 action 属性中。
所以,我的问题:

  • How does Django insert the action?
  • Why do they do it like this?
  • How can I find out what the action of this form is?
  • Django 是如何插入动作的?
  • 他们为什么这样做?
  • 我怎样才能知道这个表格的作用是什么?

Update : I see this is not a Django thing at all, but what most browsers do.

更新:我认为这根本不是 Django 的事情,而是大多数浏览器所做的事情。

回答by yaauie

Having an action of an empty string in most browsers points the form at the URL the browser currently has loaded, which is the script that served the form in the first place.

大多数浏览器中的空字符串操作会将表单指向浏览器当前加载的 URL,这是首先为表单提供服务的脚本。

回答by Nobita

For an interesting insight on forms with empty actionsread this thread, which gives you an updated HTML5-perspective on this matter.

有关具有空操作的表单的有趣见解,请阅读此线程,它为您提供了有关此问题的更新的 HTML5 视角。

回答by jottos

It's also possible that javascript loaded with this page could be setting an action once the page is loaded based on what application is using the page.

根据正在使用该页面的应用程序,该页面加载后的 javascript 也可能会在页面加载后设置操作。

Another likely possibility is that the javascript is handling the onsubmit event. One might do that to prevent the page from reloading or redirecting to a specific page

另一种可能是 javascript 正在处理 onsubmit 事件。人们可能会这样做以防止页面重新加载或重定向到特定页面

回答by CuriousCoder

I guess it's bit late to answer this post. Anyways, I'll what I learnt about this.

我想现在回答这个帖子有点晚了。不管怎样,我会我学到了什么。

If the "action" is not specified in forms, then the Django looks up the HttpResponseRedirectin the corresponding view.

如果表单中没有指定“action”,那么 Django 会HttpResponseRedirect在相应的视图中查找。

For example, in the example below:

例如,在下面的例子中:

if form.is_valid(): # All validation rules pass
        # Process the data in form.cleaned_data
        # ...
        return HttpResponseRedirect('/thanks/')

Once, the form is validated (and processed), the page is redirected to 'thanks'

一旦表单被验证(和处理),页面被重定向到“谢谢”