django表单实时选择值字段

时间:2019-04-10 16:00:10

标签: javascript python json ajax django

我创建了一个简单的Django表单,并且一切正常。 我的问题是选择领域的andress,我想得到 动态值(来自表和床头的实时值),而无需随时刷新页面至 获取最后一个字段的更改值。

如何使用AJAX进行此操作,或者使用表andress中的数据创建JSON URL 并从json获取值?

有人来帮助我吗?

models.py

class andress(models.Model):
    field_1 = models.CharField(max_length=100, blank=True, null=True)
    field_2 = models.CharField(max_length=150, blank=True, null=True)
    field_3 = models.CharField(max_length=254, blank=True, null=True)


class customer(models.Model):
    field1 = models.CharField(max_length=100, blank=True, null=True)
    field2 = models.CharField(max_length=254,blank=True, null=True)
    andress_field= models.ForeignKey('andress', blank=True)
    createddate = models.DateTimeField(default=timezone.now)

forms.py

class customerForm(forms.ModelForm):
    andress_field= forms.ModelChoiceField(queryset=andress.objects.all())

    class Meta:
        model = customer
        fields = (_all_)

views.py

def add_customer(request):
    if request.method == "POST":
        form = customerForm(request.POST)
        if form.is_valid():
            note = form.save(commit=False)
            note.save()
            return HttpResponseRedirect('/customer/success/')
    else:
        form = customerForm()
    return render(request, 'add_customer.html',{'form':form})

html:

<form enctype="multipart/form-data" method="post" action="">{% csrf_token %}
<table>
      {{ form }}
</table>
<input value="add" type="submit"/>
</form>

0 个答案:

没有答案