django - 当视图需要参数时,表单操作参数中包含什么?
这就是我所拥有的:
myview.py
,其视图采用参数user
:
def myview(request, user):
form = MyForm(request.POST)
....
return render_to_response('template.html',locals(), context_instance=RequestContext(request))
user
通过网址传递。
urls.py
:
...
urlpatterns += patterns('myview.views',
(r'^(?P<user>\w+)/', 'myview'),
)
...
我还有一个带有表单的 template.html
:
<form name="form" method="post" action=".">
...
</form>
如果 myview
函数需要参数,表单操作参数中会包含什么内容?
现在我有 action="."
。我问的原因是因为当我填写表单(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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想显式设置操作,假设您的模板中有一个变量 username,
或者您可以在 urls.py 中为 url 分配一个名称,这样您就可以像这样引用它:
If you want to explicitly set the action, assuming you have a variable username in your template,
or you could assign a name for the url in your urls.py so you could reference it like this:
您将发布到也提供该表单的同一视图。因此,首先,视图被调用并为表单服务。当您发布表单时,会调用相同的视图,但这次您要处理表单。这就是为什么动作是空的。
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.
它不应该要求任何东西。假设您位于以下网址:
您的表单应为:
此时,您已经在
myview
中,用户为johnsmith
。您的视图应如下所示:It should not require anything. Assuming you are at the following url:
Your form should be:
At this point, you are already in
myview
with userjohnsmith
. Your view should look like the following:您可以使用 request.path ,这在大多数情况下都有效。
You can use
request.path
and that will work in most cases.