Django:初始值

时间:2018-09-28 12:41:49

标签: django

我想将字段的初始值传递给未填写的表格。另一方面,如果查询(#Ref)到达,则应从数据库中预填充值。

为什么两种情况下都不显示值?

views.py:

def mieter(request,id):
    if request.method == 'POST':
        form = MieterForm(request.POST)
        if form.is_valid():
            tmp = request.GET.get('wohnungsgruppenname')
            mieter = form.save()
            Wohnungseinheit = Wohnungseinheiten.objects.get(id=tmp)
            Wohnungseinheit.mieter.add(mieter)
            return render(request,'Immo/user/mieter.html',{'form':form})
    else:
        if str(id).isdigit():
            #Ref
            user = ImmoUser.objects.get(username=request.user.username)
            form = Mieter.objects.get(pk=id)
            return render(request,'Immo/user/mieter.html',{'form':form})
        else:
            form = MieterForm(initial={'nameL':"initialValueHere",})
            return render(request,'Immo/user/mieter.html',{'form':form})

models.py:

class MieterForm(forms.ModelForm):
    nameL = forms.CharField(required=True,max_length=100)
    class Meta:
        model=Mieter
        fields = ("nameL",)

mieter.html:

<td><input class="form-control"  id="{{ form.nameL.auto_id }}" name="nameL"  value="{{ form.nameL.value }}" type="text"></td>           

0 个答案:

没有答案