django - 当视图需要参数时,表单动作参数会发生什么? [英] django - what goes into the form action parameter when view requires a parameter?

查看:105
本文介绍了django - 当视图需要参数时,表单动作参数会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的:



myview.py 具有参数用户

  def myview(request,user):
form = MyForm(request.POST)
....
return render_to_response('template.html',locals(),context_instance = RequestContext(request))
/ pre>

用户通过一个URL。



urls.py

  ... 

urlpatterns + = patterns('myview.views',
(r'^(?P< user> \w +)/','myview'),


...

我还有一个 template.html 表单:

 < form name =formmethod =postaction = > ; 
...
< / form>

如果 myview 函数需要一个参数?



现在我有 action =。。我问的原因是因为当我填写表单In(templates.html)并单击提交按钮时,我看到绝对没有从该表单传递的字段值。当我点击提交按钮时,几乎就像我传递一个空的表单。有任何想法吗?谢谢!

解决方案

您正在发布同样的视图,也提供表单。所以起初,这个观点被称为和服务于形式。当您发布表单时,相同的视图被调用,但是这次您处理该窗体。这就是为什么这个动作是空的。


This is what I have:

myview.py with a view that takes a parameter user:

def myview(request, user):
   form = MyForm(request.POST)
   ....
   return render_to_response('template.html',locals(), context_instance=RequestContext(request))

The user gets passed through an url.

urls.py:

...

urlpatterns += patterns('myview.views',
    (r'^(?P<user>\w+)/', 'myview'),
)

...

I also have a template.html with a form:

<form name="form" method="post" action=".">
...
</form>

What goes in the the form action parameter if myview function requires a parameter?

Right now I have action="." . The reason I'm asking is because when I fill up the form In (templates.html) and click the submit button I see absolutely no field values passed from that form. It's almost like I'm passing an empty form when I click the submit button. Any ideas? Thank you!

解决方案

You are posting to the same view that also serves the form. So at first, the view is called and serves the form. When you post the form, the same view gets called but this time you process the form. That's why the action is empty.

这篇关于django - 当视图需要参数时,表单动作参数会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆