Using Jquery Ajax to implement the two-level linkage of a select form under Django __jquery

Source: Internet
Author: User
using jquery Ajax to implement the two-level linkage of a select form under Django

Implemented using Django+jquery:

On the internet also see many examples, comprehensive many examples, after trying to achieve a simple provincial and municipal two-level linkage, write down, lest forget, there is no serious consideration of the place also please correct:

The first is model design (database design):

# Coding:utf-8 from
django.db import Models

# Create your models here.

#省份表
Class province (models. Model):
    name = models. Charfield (max_length=30)

    def __unicode__ (self): return
        self.name

#城市表
class city (models. Model):
    name = models. Charfield (max_length=40)
    province = models. ForeignKey (province)

    def __unicode__ (self): return
        self.name

#这个主要是用来显示, selected result
class SELECTP ( Models. Model):
    province = models. ForeignKey (province) City
    = models. ForeignKey (city)

The form is used for this purpose

# Coding:utf-8 from

Django import forms from
. Models import SELECTP

class Selectform (forms. Modelform):
    class Meta:
        model = selectp
        fields = ' __all__ '

Here is the views code:

# Coding:utf-8 from django.shortcuts import Render_to_response, get_object_or_404, redirect, Urlresolvers, RequestContext from Django.core import serializers from django.http import httpresponse import Models Province

CtP from. Forms Import Selectform # Create your views here. #获取首页 def index (request): If Request.method = ' POST ': Form = selectform (Request. POST) if Form.is_valid (): Form.save () return redirect (Urlresolvers.reverse (' select:select_ Index ')) Else:return HttpResponse (' error ') Else:pro = Province.objects.all () se
                                  = SelectP.objects.all () return render_to_response (' ajaxselect/index.html ', {' province ':p ro, ' select ': se}, Context_instance=requestcontext (Request) #ajax获取数据 def GetData (request): PK = Request. get[' PK '] province = get_object_or_404 (province, pk=pk) Citys = Province.city_set.all () data = Serializers.ser Ialize (' JSON', Citys return HttpResponse (data, content_type= ' Application/json ') 

The URL is as follows:

Urlpatterns = Patterns (' apps.ajaxselect.views ',
            url (r ' ^index/$ ', ' Index ', name= ' Select_index '),
            url (r ' ^ getdata/$ ', ' GetData ', name= ' GetData ')
            

Test the JSON data by linking to the page as follows:

[{"PK": 3, "model": "Ajaxselect.city", "fields": {"Province": 2, "name": "\U6210\U90FD"}}, {"PK": 4, "model": "Ajaxselect.  City "," fields ": {" Province ": 2," name ":" \u7ef5\u9633 "}}, {" PK ": 5," model ":" Ajaxselect.city "," fields ": {" Province ": 2, ' Name ': ' \u4e50\u5c71 '}}]

The template page is designed as follows:

<!  DOCTYPE html> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.