Django Forgienkey fields are processed in the foreground with JS

Source: Internet
Author: User

In my project there is a choice of provincial cities, the relationship between the two fields is a one-to-many relationship
Class Province (models. Model): # Capital
Name = models. Charfield (max_length=30)

Class city (models. Model): #城市
Name = models. Charfield (max_length=30)
Province = models. ForeignKey (province)

Front Code:
<link rel= "stylesheet" type= "Text/css" href= "/site_media/css/imgareaselect-default.css"/>
<script type= "Text/javascript" src= "/site_media/scripts/jquery.min.js" ></script>
<script type= "Text/javascript" src= "/site_media/scripts/jquery.imgareaselect.pack.js" ></script>

<form action= "/addinfo/" method= "post" name= "class=" Hform ">
<p><label><span style= "color:red" >*</span> Select Category </label>
<select name= "province" id= "province" >
<option value= "" > Please select Category </option>
{% for province in provinces%}
<option value= "{{}}" >{{}}</option>
<p><label<span style= "color:red" >*</span> Select sub-class </label>
<select name= "City" id= "City" >
<p><label><span style= "color:red" >*</span> title </label>
<input type= "text" name= "title" id= "title" >

<p><input type= "Submit" name= "Submit" value= "Submission" class= "button" ></p>


{% block JS%}
<script lang= "JavaScript" >

$ ("#province"). Change (function () {
if ($ ("#province"). val ()! = "") {

$.getjson ("/getcity/", {Category: $ ("#province"). Val ()},
function (data) {
$ ("#city"). empty ();
$ ("#city"). Append ("<option value=\" \ "> Please select sub-category </option>");
if (data.length!=0) {
$.each (Data,function (i,n) {
$ ("#city"). Append ("<option value=" +i+ ">" +n+ "</option>");


$ ("#city"). empty ();


$ ("form"). Submit (function () {

if ($ ("#province"). val () = = "") {
Alert ("Please select category");
$ ("#province"). focus ();
return false;
if ($ ("#city"). val () = = "") {
Alert ("Please select Subclass");
$ ("#city"). focus ();
return false;


def cityinfo (Request):
Provinces = Province.objects.all ()
Return Render_to_response ("cityform.html", locals ())
def getcity (Request):
Provinceid = Int (request. Get.get (' category '))
Province = Province.objects.get (Id=provinceid)

Citys = City.objects.filter (province=province)

data = {}

For City in Citys:
Data[] =

Return HttpResponse (Simplejson.dumps (data))
(R ' ^province/$ ', views.cityinfo),
     (R ' ^getcity/$ ', views.getcity),
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: 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.