我正在尝试将状态更改为“成分”。 当我单击“ indienen”按钮时,出现此错误: 无法通过Weekstaat实例访问Manager。 如何解决此错误?------------
这是我的index.html
<form action="{% url 'weekstaat-indienen' Weekstaat.id %}" method=POST>
{% csrf_token %}
<button id="approve-btn" type="submit">Approve</button>
</form>
</td>
<td class="text-center">
<button type="button" class="read-Contract btn btn-sm btn-primary" data-id="{% url 'read_Weekstaat' Weekstaat.id %}">
<span class="fa fa-eye"></span>
</button>
<button type="button" class="update-Contract btn btn-sm btn-primary" data-id="{% url 'update_Weekstaat' Weekstaat.id %}">
<span class="fa fa-pencil"></span>
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p class="text-primary">U heeft nog geen Weekstaat</p>
{% endif %}
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function bevestigIndienen(){
if(confirm('U staat op het punt een weekstaat in te dienen. Weet u het zeker?') != true){
document.getElementById('formulierIndienen').action = '';
window.location.href = "{% url 'home' %}";
}
}
这是我的观点。py
def indienen(request, id):
global Weekstaat
Weekstaat = Weekstaat.objects.get(pk=id)
Weekstaat.Status = 'Ingedient'
Weekstaat.save()
return redirect('../../weekstaat')
这是我的模型。py
class Weekstaat(models.Model):
id = models.AutoField(primary_key=True, null=False, blank=False)
status = models.CharField(max_length=11, choices=STATUS_CHOICES, default='Niet ingediend',)
jaar = models.ForeignKey(Jaar, default=datetime.now().year, null=False, blank=False,on_delete=models.CASCADE)
week = models.ForeignKey(Week, default=date.today().isocalendar()[1], null=False, blank=False,on_delete=models.CASCADE)
werknemer = models.ForeignKey(User, related_name='weekstaat_werknemer', null=False, blank=False, default=1,on_delete=models.CASCADE)
maandag = models.FloatField(null=True, blank=True, default=0)
dinsdag = models.FloatField(null=True, blank=True, default=0)
woensdag = models.FloatField(null=True, blank=True, default=0)
donderdag = models.FloatField(null=True, blank=True, default=0)
vrijdag = models.FloatField(null=True, blank=True, default=0)
zaterdag = models.FloatField(null=True, blank=True, default=0)
zondag = models.FloatField(null=True, blank=True, default=0)
def __str__(self):
return str(self.jaar) + ' week ' + str(self.week) + str(self.werknemer) + str(self.status)
这是我的urls.py
urlpatterns = [
path('', views.Index.as_view(), name='indexs'),
path('create/', views.WeekstaatCreateView.as_view(), name='create_Weekstaat'),
path('update/<int:pk>', views.WeekstaatUpdateView.as_view(), name='update_Weekstaat'),
path('read/<int:pk>', views.WeekstaatReadView.as_view(), name='read_Weekstaat'),
path('delete/<int:pk>', views.WeekstaatDeleteView.as_view(), name='delete_Weekstaat'),
path('indienen/<int:id>', views.indienen, name='weekstaat-indienen'),
]
答案 0 :(得分:1)
在函数indienen
中,您用Weekstaat
实例覆盖了全局变量Weekstaat
:
def indienen(request, id):
global Weekstaat
Weekstaat = Weekstaat.objects.get(pk=id)
...
该方法第二次运行时,您正在尝试通过实例获取Manager。这是不可能的,因此错误消息。尝试改用另一个变量名:
def indienen(request, id):
global Weekstaat
weekstaat = Weekstaat.objects.get(pk=id) # notice lowercase w
weekstaat.Status = 'Ingedient'
weekstaat.save()
return redirect('../../weekstaat')
答案 1 :(得分:1)
您的代码似乎不合适。如下更改视图,
def indienen(request, id):
weekstaat_instance = Weekstaat.objects.get(pk=id)
weekstaat_instance.Status = 'Ingedient'
weekstaat_instance.save()
return redirect('../../weekstaat')
答案 2 :(得分:0)
该错误是由Weekstaat.Status = 'Ingedient'
处的错字引起的,应为Weekstaat.status = 'Ingedient'
。注意:小写