我正在使用Python3.7,Django 2.2。
下面您可以找到我的代码,以更好地了解我的问题。
如您在下面看到的代码所示,我创建了带有一些记录的表。跳过models.py中值的波兰语名称。我们只需要关注manager_accpt。该值具有2个参数(请检查文件choices.py)。
当我使用def manager_day_free_accpt创建视图时,会得到多个对象。最后一列包含选择,我的用户必须选择并保存。我在下面的屏幕中放置了示例。 我怎么了 我必须以2种方式来做,因为用户会选择,哪种解决方案对他们来说更友好:) 我的按钮有问题,应该放在哪里?因为现在,提交更改了整个对象,但其值有误。
当我将Button置于“ for loop”之外时,对象什么也没有发生。您能帮我吗,我必须找到两个解决方案:
第一: 每行应包含带有提交和拒绝的绿色按钮(白色按钮)。并且,当用户单击绿色按钮时,仅此原始值中的值应更新db中的对象。
第二溶胶: 用户在每一行中选择接受和拒绝。之后,用户应按下“提交”按钮,所有更改应更新db中的对象。
models.py
class Wniosek_urlop(models.Model):
data_zloz_wniosku = models.DateTimeField(auto_now_add=True, null=True)
data_start_urlop = models.DateField('Urlop od', blank=False, null=True)
data_end_urlop = models.DateField('Urlop do', blank=False,null=True)
#Representant - osoba zastepująca w tym czasie nieobecnego pracownika.
representant = models.CharField('Zastępca', max_length=50, null=False,blank=False,
help_text="Podaj imie i nazwisko osoby zastępującej Cię w czasie nieobecności. To pole będzie widoczne przez kierownika")
reason = models.TextField('Uzasadnienie', max_length=255, blank=False, null=False, help_text="Powód złożenia wniosku", error_messages={'blank': "Wpisz tutaj coś"})
note_optional = models.TextField('Uwagi', max_length=255, null=True, blank=True)
urlop = models.CharField('Urlop', choices=urlop_ch, default=1, max_length=50)
manager = models.CharField('Przełożony', choices=manager_ch, default=0, max_length=50)
manager_accpt = models.CharField('Akceptacja', choices=accpt_ch, null=True, blank=True, max_length=25)
worker = models.ForeignKey(Lista, on_delete=models.CASCADE, null=True, blank=True)
views.py
def manager_day_free_accpt(request):
db_all_free_days = Wniosek_urlop.objects.all().order_by('id')
#Pole wyboru y_n
form_accpt = Manager_accpt_y_n(request.POST or None)
if form_accpt.is_valid():
form_accpt.save()
messages.success(request, 'Zapisano zmiany.')
else:
messages.success(request, 'Ups. Cos poszło nie tak.')
return render(request, "manager_accpt.html", {'db_all_free_days': db_all_free_days, 'form_accpt': form_accpt})
choices.py
manager_ch = [("1", "manager 1"),
("2", "manager 2")
]
accpt_ch = [
("1", "accept"),
("2", "reject")
]
manager_accpt.html
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Pracownik</th>
<!--Skip some col-->
<th scope="col">Przełożony</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for tmp in db_all_free_days %}
{%if tmp.manager_accpt == null %}
<tr>
<th scope="row">{{tmp.id}}</th>
<td>{{tmp.worker}}</td>
<td>{{tmp.manager}}</td>
<td>
<form role="form">
{% csrf_token %}
{{form_accpt|bootstrap}}
<button type="submit" class="btn btn-success">Wyślij wniosek <i class="far fa-save"></i></button>
<button type="button" class="btn btn-outline-secondary" onclick="goBack()">Cofnij</button>
</form>
</td>
</tr>
{% else %}
<div class="alert alert-dark" role="alert">
No free days to accept
</div>
{% endif %}
{% endfor %}