Django:在提交表单后发送GET-param

时间:2018-09-24 10:45:13

标签: django

我用http://localhost:63314/user/mieter/?wohnungseinheit=1作为(获取)参数调用页面。

我想使用传输的参数作为“ wohnungseinheit”的分配。我填写并发送“表格”后,缺少GET参数。

如何分配“ wohnungseinheit”?

def mieter(request,id):
    if request.method == 'POST':
        form = MieterForm(request.POST)
        if form.is_valid():
            tmp = request.GET.get('wohnungseinheit')
            mieter = form.save()#wohnungseinheit=tmp
            print(tmp) #result: None
            Wohnungseinheit = Wohnungseinheiten.objects.get(id=tmp)
            Wohnungseinheit.mieter.add(mieter)
            return render(request,'Immo/user/mieter.html',{'form':form}) # i think i also could use render_to_response
    else:
        if not str == None and str(id).isdigit():
            #unimportant code here

            if blnAllowedAccess:
                form = MieterForm(request.POST)
                return render(request,'Immo/user/mieter.html',{'form':form})
            else:
                #NOTALLOWEDTOACCESS! TODO
                pass
        else:
            tmp = request.GET.get('wohnungseinheit')
            if tmp is not None:
                form = MieterForm(request.POST or None)
                return render(request,'Immo/user/mieter.html',{'form':form})
            else:
                pass #TODO: 404-error

编辑:

models.py:

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

class Wohnungseinheiten(models.Model):
    mieter = models.ManyToManyField(Mieter,blank=True,null=True)

urls.py:

    re_path(r'user/mieter/(?:(?P<id>\w+)/)?$', views.mieter,name="mieter"),

user / mieter.html:

<form class="form-signin" action="{% url 'mieter' %}" method="post">{% csrf_token %}
<input class="form-control"  id="{{ form.nameL.auto_id }}" name="nameL"  value="{{ form.nameL.value }} "type="text">
<button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>

1 个答案:

答案 0 :(得分:0)

解决方案是:<a href="{% url 'mieter' %}?wohnungseinheit=1">

参数可以由“ if”或“ for”动态组合。