且构网

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

django - 当视图需要参数时,表单动作参数会发生什么?

更新时间:2023-11-30 21:02:04

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


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.