效果:模板选项自动产生
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c9747c73d3fc44212ebc9066b3e8e6d9.png)
模板
<tr>
<th scope="row" for="inputGroupSelect01">申请奖品名称</th>
<td>
<select class="custom-select" required="required" name="reward_apply_name" id="reward_apply_name">
{% for rewarditem in reward_list %}
<option value='{{ rewarditem.reward_name }}'>{{ rewarditem.reward_name }}</option>
{% endfor %}
</select>
</td>
</tr>
models.py
class reward(models.Model):
id = models.AutoField(primary_key=True)
reward_name = models.CharField(max_length=135, blank=True, null=True, verbose_name=u'奖品名称')
reward_image = models.ImageField(max_length=135, blank=True, null=True, verbose_name=u'奖品照片',upload_to='static\images')
reward_score = models.IntegerField(blank=True, null=True,verbose_name=u'奖品积分')
reward_inventory_total = models.IntegerField(blank=True, null=True, verbose_name=u'奖品入库总量')
reward_inventory_used = models.IntegerField(blank=True, null=True, verbose_name=u'奖品使用量')
reward_inventory_left = models.IntegerField(blank=True, null=True, verbose_name=u'奖品剩余量')
class Meta:
db_table = u'reward'
verbose_name = u'奖品'
verbose_name_plural = u'奖品'
def __str__(self):
return self.reward_name
class reward_apply(models.Model):
id = models.AutoField(primary_key=True)
reward_apply_employeeid = models.CharField(max_length=135, blank=True, null=True, verbose_name=u'申请者工号')
reward_apply_employeename = models.CharField(max_length=135, blank=True, null=True, verbose_name=u'申请者姓名')
reward_apply_name = models.CharField(max_length=135, blank=True, null=True, verbose_name=u'申请奖品名称')
reward_apply_name_fk = models.ForeignKey(reward, blank=True, null=True, on_delete=models.CASCADE,related_name='reward_apply')
reward_apply_score = models.IntegerField(blank=True, null=True,verbose_name=u'申请奖品积分')
reward_apply_number = models.IntegerField(blank=True, null=True, verbose_name=u'申请奖品数量')
reward_apply_datetime = models.DateTimeField(blank=True, null=True, verbose_name=u'申请奖品时间')
reward_apply_reviewByCI = models.CharField(max_length=135, blank=True, null=True,choices=(('朱慧敏','朱慧敏'),), verbose_name=u'CI审核者')
reward_apply_finished = models.CharField(max_length=135, blank=True, null=True,choices=(('审核中','审核中'),('已核准','已核准'),('已拒绝','已拒绝')), verbose_name=u'申请奖品状态')
class Meta:
db_table = u'reward_apply'
verbose_name = u'奖品申请'
verbose_name_plural = u'奖品申请'
def __str__(self):
return self.reward_apply_employeeid + '申请' + self.reward_apply_name
views.py
@login_required
def reward_apply_by_employee(request):
'''员工申请奖品兑换'''
reward_list = reward.objects.all()
print(request.POST)
reward_apply_employeeid = request.POST.get('reward_apply_employeeid', '')
reward_apply_employeename = request.POST.get('reward_apply_employeename', '')
reward_apply_name = request.POST.get('reward_apply_name', '')
reward_apply_score = request.POST.get('reward_apply_score', '')
reward_apply_number = request.POST.get('reward_apply_number','')
reward_apply_datetime = request.POST.get('reward_apply_datetime','')
reward_apply_reviewByCI = request.POST.get('reward_apply_reviewByCI','')
reward_apply_finished = request.POST.get('reward_apply_finished','')
'''更新reward_apply模型'''
if reward_apply_employeeid and reward_apply_name and reward_apply_number>=1 and reward_apply_datetime:
reward_apply_self = reward_apply.objects.create(reward_apply_employeeid = reward_apply_employeeid,
reward_apply_employeename = reward_apply_employeename,
reward_apply_name = reward_apply_name,
reward_apply_name_fk = reward.objects.filter(reward_name=reward_apply_name)[0],
reward_apply_score = int(reward.objects.filter(reward_name=reward_apply_name)[0].reward_score) * int(reward_apply_number),
reward_apply_number = reward_apply_number,
reward_apply_datetime = reward_apply_datetime,
reward_apply_reviewByCI = reward_apply_reviewByCI,
)
return HttpResponse('申请成功!')
else:
pass
context = {'reward_list':reward_list}
return render(request, 'reward_apply_by_employee.html',context=context)