DJANGO列表索引必须是整数或切片,而不是str

时间:2019-09-04 16:33:07

标签: python django django-models django-views

Views.py:

    def reserversalle(request , id):

    form= ReserverSalle(request.POST or None)
    object = models.authentifier.objects.get(id=id)
    allreservertion = models.Resasalle.objects.all()
    print(allreservertion)
    print(object)
    if form.is_valid():
        for allreservertion in allreservertion:
            print(allreservertion)
            if(allreservertion.nomsalle ==  form.cleaned_data['nomsalle'] ) :
                print("nom des salles égaux")

                if( allreservertion.datedebut ==  form.cleaned_data['datedebut']) :
                    print("meme date")

                    if(allreservertion.heuredebut == form.changed_data['heuredebut']):
                        print("meme heure début")
                        return render(request,'registration/plan.html') 



        print("gooddd")
        reserver = models.Resasalle()
        reserver.nomsalle = form.cleaned_data['nomsalle']
        reserver.motifresa = form.cleaned_data['motifresa']
        reserver.datedebut = form.cleaned_data['datedebut']
        reserver.heuredebut = form.cleaned_data['heuredebut']
        reserver.heurefin = form.cleaned_data['heurefin']
        reserver.authentifier = object

        reserver.save()



    context= { 
            'form' : form ,
        }
    return render(request,'registration/reserversalle.html', context)

models.py:

    class authentifier(models.Model):
   matricule =models.CharField(max_length=254, blank=True, null=True)
   password = models.CharField(max_length=254, blank=True, null=True)
   nom =models.CharField(max_length=254, blank=True, null=True)
   prenom=models.CharField(max_length=254, blank=True, null=True)
   statut = models.CharField(max_length=254, blank=True, null=True)

class Resasalle(models.Model):
    idrs = models.AutoField(primary_key=True)

    nomsalle = models.CharField(max_length=254, blank=True, null=True)
    motifresa = models.CharField(max_length=254, blank=True, null=True)
    datedebut = models.DateField(blank=True, null=True)
    heuredebut = models.TimeField(blank=True, null=True)
    heurefin = models.TimeField(blank=True, null=True)
    authentifier = models.ForeignKey(authentifier,on_delete=models.CASCADE , default="")

forms.py:

    class ReserverSalle(forms.Form):
    nomsalle = forms.CharField(required=True , widget=forms.TextInput)
    motifresa = forms.CharField(required=True , widget=forms.TextInput)
    datedebut = forms.DateField( initial="2019-06-21", widget=forms.SelectDateWidget(years=YEARS))
    heuredebut = forms.TimeField( initial='00:00:00')
    heurefin = forms.TimeField( initial='00:00:00')

当我尝试触发第二个条件时:      if(allreservertion.heuredebut == form.changed_data ['heuredebut']):

i解决了此错误:列表索引必须是整数或切片,而不是str  我需要一些帮助和解决方案

0 个答案:

没有答案