【发布时间】:2019-05-14 13:56:08
【问题描述】:
我有一个模型叫ItemBatch
# item upload
class ItemBatch(models.Model):
ttypes =(('Open','Open'),('Container','Container'),('Trailer','Trailer'),('All','All'))
uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='uploaded_by')
name = models.CharField(max_length=30)
pid = models.IntegerField(blank=True)
quantity = models.IntegerField(blank=True)
length = models.FloatField(blank=True)
width = models.FloatField(blank=True)
height = models.FloatField(blank=True)
volume = models.FloatField(blank=True)
weight = models.FloatField(blank=True)
truck_type = models.CharField(max_length=255,default=0, choices=ttypes)
origin = models.CharField(max_length=100, blank=True)
destination = models.CharField(max_length=100, blank=True)
time = models.DateTimeField(max_length=100, blank=True,default=now)
rtd = models.BooleanField(default=False) #ready to dispatch checkbox
def __str__ (self):
return self.name
我正在使用这个视图函数来渲染它:
@method_decorator([login_required, teacher_required], name='dispatch')
class UploadedItems(ListView):
model = ItemBatch
ordering = ('name',)
context_object_name = 'items'
template_name = 'classroom/teachers/item_list.html'
def get_queryset (self):
return ItemBatch.objects.filter(uploaded_by=self.request.user)
我在模板中渲染这个表格并得到这个:
这是模板中的代码:
{% for quiz in last %}
<tr>
<form method="post" novalidate>
{% csrf_token %}
<td class="align-middle"><input type="checkbox" value="{{ quiz.pid }}"></td>
<td class="align-middle">{{ quiz.name }}</td>
<td class="align-middle">{{ quiz.pid }}</td>
<td class="align-middle">{{ quiz.quantity }}</td>
<td class="align-middle">{{ quiz.length }}x{{ quiz.width }}x{{ quiz.height }}</td>
<td class="align-middle">{{ quiz.volume }}/{{ quiz.weight }}</td>
<td class="align-middle">{{ quiz.origin }}</td>
<td class="align-middle">{{ quiz.destination }}</td>
<td class="align-middle">{{ quiz.time|naturaltime }}</td>
</form>
</tr>
{% empty %}
我尝试了什么
如您所见,我在表格中创建了一个表单,还包括了一个复选框。但是,我无法以任何方式获取该复选框的输出。如何让用户选择一些项目,并以任何方式将数据返回给我,以便我可以使用它的另一个功能?如果复选框只能给我 id 或 pk 值,我将能够使用适当的对象将其反转,但我无法在已经是另一个函数的输出渲染的表上创建输入表单。这是正确的方法吗?
【问题讨论】:
标签: django checkbox django-models django-forms django-views