在我的Django模板中使用复选框表单时没有POST响应 [英] No POST response when using checkbox form in my Django template

查看:238
本文介绍了在我的Django模板中使用复选框表单时没有POST响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我试图删除由用户选择的事件,使用
复选框来检查他们想要删除哪些事件。但对于
一些原因每当我调用request.POST.get('event_list')没有什么是
收到即使框被选中,我最终什么也没有。
这是我的模板和视图,应删除所选的
事件。

  {%if event_list%} 
{%for event_list%}
{%csrf_token%}
< input type =checkboxname =event_list
id =event { {forloop.counter}}/>
< label for =event {{forloop.counter}}> {{event.title}}< /
label>< br /
{%endfor%}
< input type ='submit'value ='delete checked'>
< / form>
< p> {{remove}}< p /> {%comment%}这是应该
删除的{%endcomment%}
{%if delete_error%}
< p> {{delete_error}}< / p>
{%endif%}

views.py

  def EventDelete(request):
remove = request.POST.get('event_list')
如果删除:
删除。 delete()
else:
delete_error =你没有删除任何东西
return redner_to_response(detail.html,{'delete_error':
delete_error,'remove' :remove},
context_instance = RequestContext(request))

没有什么东西,不应该有
的事件的标题吗?不幸的是我不知道很多关于html和它的工作:(我真的很感谢帮助:)我觉得这是一个简单的修复,只是失去一个小细节。感谢:)

解决方案

复选框没有值,所以你只需要发送到服务器。 >

This:

 < input type =checkboxname =event_list 
id =event {{forloop.counter}}/>

应显示

 < input type =checkboxname =event_list
id =event {{forloop.counter}}value ={{forloop.counter}}

然后(一旦服务器接收到一个id列表) ,你需要以某种方式加载ids列表,并制定出什么删除。您可能想要类似

  remove = request.POST.get('event_list')

event = get_object_or_404(Event,pk = id)
event.delete()

我想你有一个公平的点学习!请查看 firebug 以监控实际发送到您的服务器的内容。转到 w3schools了解表单,并阅读django文档以处理删除。 / p>

Hey there, I am trying to delete Events as chosen by a by a user using check boxes to check of which events they want to be deleted. But for some reason whenever I call request.POST.get('event_list') Nothing is received even though boxes are checked and I end up with nothing. Here is my template and the view that should be deleting the chosen events.

 {% if event_list %}
       {% for event in event_list%}
               {%csrf_token%}
               <input type="checkbox" name="event_list"
id="event{{ forloop.counter }}" />
               <label for="event{{ forloop.counter }}">{{ event.title }}</
label><br />
       {% endfor %}
       <input type = 'submit' value = 'delete checked'>
       </form>
       <p>{{removal}}<p/>    {%comment%} this is what should be
removed{%endcomment%}
       {% if delete_error %}
               <p>{{delete_error}}</p>
           {% endif %}

views.py

def EventDelete(request):
       removal = request.POST.get('event_list')
       if removal:
               removal.delete()
       else:
               delete_error = "You didn't delete anything"
       return redner_to_response("detail.html", {'delete_error':
delete_error, 'removal': removal},
context_instance=RequestContext(request))

Im not sure why removal doesn't have anything in it, shouldn't it have the titles of the events in it? Unfortunately I don't know much about html and its workings :( I would really appreciate the help :) I feel like it is a simple fix and im just missing a small detail. Thanks :)

解决方案

The checkboxes have no value so you'll just get "on" sent to the server.

This:

<input type="checkbox" name="event_list"
 id="event{{ forloop.counter }}" />

should read

<input type="checkbox" name="event_list"
 id="event{{ forloop.counter }}" value="{{ forloop.counter }}" />

And then (once the server has received a list of ids), your code for processing it looks wrong, you need to somehow load the list of ids, and work out what to delete. You probably want something like

removal = request.POST.get('event_list')
for id in removal:
     event = get_object_or_404(Event, pk=id)
     event.delete()

I think you have a fair bit to learn! Check out firebug for monitoring what is actually being sent back to your server. Go to w3schools to learn about forms, and read the django documentation for handling the deletion.

这篇关于在我的Django模板中使用复选框表单时没有POST响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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