Django 从网页的条目列表中删除数据库条目
我试图从一个有多个单选按钮的网页中获取用户输入,我需要从员工数据库中删除所选的单选按钮数据 在我看来,, views.py 在我的listemp.html中,我有这个模板Django 从网页的条目列表中删除数据库条目,django,templates,radio-button,Django,Templates,Radio Button,我试图从一个有多个单选按钮的网页中获取用户输入,我需要从员工数据库中删除所选的单选按钮数据 在我看来,, views.py 在我的listemp.html中,我有这个模板 <!doctype Html> <html> <head> </head> <body> <form method='POST' action='/deleted'>
<!doctype Html>
<html>
<head>
</head>
<body>
<form method='POST' action='/deleted'>
{% csrf_token %}
<p>
{% for e in listofemp %}
<li><a href= "{{e.pk}}">{{ e.name }} </a></li> {{ e.depart }}
<input type='radio' name="e" id="e{{forloop.counter}}" value= "{{e.pk }}" />
{% endfor %}
</p>
<input type='submit' value=''/>
</form>
<li><a = href='/alldetails'> List all the Details together <a/></li>
</body>
</html>
问题可能是因为您将所有收音机的名称设置为相同,即名称e。尝试为for循环中的多个收音机指定不同的名称。您能公布您得到的确切回溯吗?并将print request.POST作为if块的第一行,然后发布输出?我收到了一个GET请求。我试过使用GET,它在e处说MutiDictError。另外,print request.POST gives和print request.POST gives因为你得到了一篇文章,我想这是因为你在没有斜杠的情况下发布到/deleted,Django重定向到/deleted/,重定向总是GET。更改动作URL.Wow。知道这一点很有趣。它将在未来对我有所帮助,但不管怎样,我重新设计了我的代码,我的时间不够,我只需要它来工作。我现在使用URL链接而不是单选按钮来删除。我对Django很陌生,5天大的名字不一样。这有一个计数器。名称是e1、e2、e3、e4、e5、e5等…您使id不同,而不是名称。它是在POST字典中传递的名称。我是指输入字段的名称。
<!doctype Html>
<html>
<head>
</head>
<body>
<form method='POST' action='/deleted'>
{% csrf_token %}
<p>
{% for e in listofemp %}
<li><a href= "{{e.pk}}">{{ e.name }} </a></li> {{ e.depart }}
<input type='radio' name="e" id="e{{forloop.counter}}" value= "{{e.pk }}" />
{% endfor %}
</p>
<input type='submit' value=''/>
</form>
<li><a = href='/alldetails'> List all the Details together <a/></li>
</body>
</html>
from django.db import models
class Emp(models.Model):
name = models.CharField(max_length=200)
depart = models.CharField(max_length= 16, choices = (('FINANCE','FINANCE'),('MARKETING','MARKETING')))
timestamp = models.DateTimeField()
update = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return self.name