如何从django表单发出ajax请求并加载响应数据

时间:2015-11-11 09:47:51

标签: jquery python ajax django

这是我的forms.py

from django import forms
from django.utils.translation import ugettext_lazy as _
from category.models import *

class ItemForm(forms.Form):

    category = forms.ModelChoiceField(queryset=Category.objects.filter(is_active=True).order_by('category_text'),label=_("Category"))
    subcategory = forms.ModelChoiceField(queryset=Subcategory.objects.all().order_by('subcategory_text'),label=_("Subcategory"))
    item = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Item")

这是models.py

from django.db import models


class Category(models.Model):
    category_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    is_active = models.BooleanField()
    def __str__(self):
        return self.category_text


class Subcategory(models.Model):
    category = models.ForeignKey(Category)
    subcategory_text = models.CharField(max_length=200)
    def __str__(self):
        return self.subcategory_text

我想首先从category选择dropdown,然后根据category subcategorycategory我要加载的Subcategory ajax }`dropdown。

我不知道如何从request发出django form dropdown并将响应数据加载回form中的/

提前感谢您的帮助。

0 个答案:

没有答案