django - 当视图需要参数时,表单操作参数中包含什么?

发布于 2024-10-27 04:25:54 字数 775 浏览 0 评论 0原文

这就是我所拥有的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肩上的翅膀 2024-11-03 04:25:54

如果您想显式设置操作,假设您的模板中有一个变量 username

<form name="form" method="post" action="{% url myview.views username %}">

或者您可以在 urls.py 中为 url 分配一个名称,这样您就可以像这样引用它:

# urls.py
urlpatterns += patterns('myview.views',
    url(r'^(?P<user>\w+)/', 'myview', name='myurl'), # I can't think of a better name
)

# template.html
<form name="form" method="post" action="{% url myurl username %}">

If you want to explicitly set the action, assuming you have a variable username in your template,

<form name="form" method="post" action="{% url myview.views username %}">

or you could assign a name for the url in your urls.py so you could reference it like this:

# urls.py
urlpatterns += patterns('myview.views',
    url(r'^(?P<user>\w+)/', 'myview', name='myurl'), # I can't think of a better name
)

# template.html
<form name="form" method="post" action="{% url myurl username %}">
贩梦商人 2024-11-03 04:25:54

您将发布到也提供该表单的同一视图。因此,首先,视图被调用并为表单服务。当您发布表单时,会调用相同的视图,但这次您要处理表单。这就是为什么动作是空的。

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.

迷乱花海 2024-11-03 04:25:54

它不应该要求任何东西。假设您位于以下网址:

www.yoursite.com/users/johnsmith/

您的表单应为:

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

此时,您已经在 myview 中,用户为 johnsmith。您的视图应如下所示:

if request.method == 'POST':
    form = MyForm(request.POST)
    if form.is_valid():
        # you should be able to extract inputs from the form here
else:
    form = MyForm()

It should not require anything. Assuming you are at the following url:

www.yoursite.com/users/johnsmith/

Your form should be:

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

At this point, you are already in myview with user johnsmith. Your view should look like the following:

if request.method == 'POST':
    form = MyForm(request.POST)
    if form.is_valid():
        # you should be able to extract inputs from the form here
else:
    form = MyForm()
过期情话 2024-11-03 04:25:54

您可以使用 request.path ,这在大多数情况下都有效。

You can use request.path and that will work in most cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文