在django中引用多个提交按钮

时间:2022-09-25 15:31:11

How would I call on the following multiple submit forms --

我如何致电以下多个提交表格 -

<form action="/" method="post">
    {% csrf_token %}

    <input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit"></td>
    <td><input type="submit" value="Delete"></td>
</tr>
</form>

I want to do something like this --

我想做这样的事情 -

if value = edit:
    do this

if value = delete:
    do this

How would I code this in the views.py file?

我将如何在views.py文件中对此进行编码?

4 个解决方案

#1


29  

Give the input types a name and look for them in your request.POST dictionary.

为输入类型命名并在request.POST字典中查找它们。

E.g.:

例如。:

<form action="/" method="post">
    {% csrf_token %}

    <input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit" name="_edit"></td>
    <td><input type="submit" value="Delete" name="_delete"></td>
</tr>

and in views.py something like

并在views.py之类的东西

if request.POST:
    if '_edit' in request.POST:
         do_edit()
    elif '_delete' in request.POST:
         do_delete()

EDIT: Changed d.has_key(k) to k in d per Daniel's comment. has_key is deprecated in python 3.0 and the in style is preferred as its more generic -- specifically d.has_key(k) fails if d isn't a dictionary, but k in d works for any d that's an iterable (e.g., dict, string, tuple, list, set).

编辑:根据Daniel的评论,将d.has_key(k)更改为k in d。 has_key在python 3.0中被弃用,并且in样式是首选的,因为它更通用 - 特别是如果d不是字典则d.has_key(k)失败,但是d中的k适用于任何可迭代的d(例如,dict, string,tuple,list,set)。

#2


6  

I know this question was asked a long time ago, but for the record here is a solution using class-based views. It uses the same html as in dr jimbob's answer.

我知道很久以前就问过这个问题,但是这里的记录是使用基于类的视图的解决方案。它使用与jimbob博士的答案相同的html。

from django.views.generic.edit import FormView

class MyView(FormView):
    template_name = 'mytemplate.html'
    form_class = MyForm

    def form_valid(self, form):
        if '_delete' in self.request.POST:
            # do delete
        elif '_edit' in self.request.POST:
            # do edit

Note the default behavior of form_valid is:

请注意,form_valid的默认行为是:

return HttpResponseRedirect(self.get_success_url())

#3


4  

You would need to give the submit buttons a name attribute, say "action" for example, then you could reference them in the request.POST collection:

您需要为提交按钮指定一个名称属性,例如“动作”,然后您可以在request.POST集合中引用它们:

def my_view(request):
    action = request.POST.get('action')
    if action == 'Edit':
        #do edit
    else:
        # do delete

Hope that helps you out.

希望能帮到你。

#4


0  

<form action="/" method="post">
<tr>
    <td><input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit" name="submit"> </td>
    <td><input type="submit" value="Delete" name="submit"> </td>
</tr>
</form>

views.py will be -

views.py将 -

def our_view(request):
    if request.POST.get('submit') == 'Edit':
        make_edit()
    elif request.POST.get('submit') == 'Delete':
        make_deletes()
    else:
        raise Http404()

#1


29  

Give the input types a name and look for them in your request.POST dictionary.

为输入类型命名并在request.POST字典中查找它们。

E.g.:

例如。:

<form action="/" method="post">
    {% csrf_token %}

    <input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit" name="_edit"></td>
    <td><input type="submit" value="Delete" name="_delete"></td>
</tr>

and in views.py something like

并在views.py之类的东西

if request.POST:
    if '_edit' in request.POST:
         do_edit()
    elif '_delete' in request.POST:
         do_delete()

EDIT: Changed d.has_key(k) to k in d per Daniel's comment. has_key is deprecated in python 3.0 and the in style is preferred as its more generic -- specifically d.has_key(k) fails if d isn't a dictionary, but k in d works for any d that's an iterable (e.g., dict, string, tuple, list, set).

编辑:根据Daniel的评论,将d.has_key(k)更改为k in d。 has_key在python 3.0中被弃用,并且in样式是首选的,因为它更通用 - 特别是如果d不是字典则d.has_key(k)失败,但是d中的k适用于任何可迭代的d(例如,dict, string,tuple,list,set)。

#2


6  

I know this question was asked a long time ago, but for the record here is a solution using class-based views. It uses the same html as in dr jimbob's answer.

我知道很久以前就问过这个问题,但是这里的记录是使用基于类的视图的解决方案。它使用与jimbob博士的答案相同的html。

from django.views.generic.edit import FormView

class MyView(FormView):
    template_name = 'mytemplate.html'
    form_class = MyForm

    def form_valid(self, form):
        if '_delete' in self.request.POST:
            # do delete
        elif '_edit' in self.request.POST:
            # do edit

Note the default behavior of form_valid is:

请注意,form_valid的默认行为是:

return HttpResponseRedirect(self.get_success_url())

#3


4  

You would need to give the submit buttons a name attribute, say "action" for example, then you could reference them in the request.POST collection:

您需要为提交按钮指定一个名称属性,例如“动作”,然后您可以在request.POST集合中引用它们:

def my_view(request):
    action = request.POST.get('action')
    if action == 'Edit':
        #do edit
    else:
        # do delete

Hope that helps you out.

希望能帮到你。

#4


0  

<form action="/" method="post">
<tr>
    <td><input type="text" name="{{ email.id }}" value=" {{email}}"></td>
    <td><input type="submit" value="Edit" name="submit"> </td>
    <td><input type="submit" value="Delete" name="submit"> </td>
</tr>
</form>

views.py will be -

views.py将 -

def our_view(request):
    if request.POST.get('submit') == 'Edit':
        make_edit()
    elif request.POST.get('submit') == 'Delete':
        make_deletes()
    else:
        raise Http404()