1

我有两个表并使用外键关联它。我想使用 jquery 显示表的值。

我的观点.py

# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from sampleapp.models import *
from django.conf import settings
import os
def personal(request):
    if request.method=='POST':
        v1=request.POST.get('category1')
        categoryquiz(category=v1).save()
        return HttpResponseRedirect('/personal')
    else:
        return render(request,'first.html')
def sectional(request):
    if request.method=='POST':
        v2=request.POST.get('drop1')
        v3=request.POST.get('section1')
        sectionquiz(var1_id=v2,section=v3).save()
        return HttpResponseRedirect('/show')
    else:
        st=categoryquiz.objects.all()
        return render(request,'second.html',{'st':st})
def show(request):
    if request.method=='POST':
        sectionquiz(var1_id=v5,section=v5).save()
        return HttpResponseRedirect('/show')
    else:
        st=categoryquiz.objects.all()
        s=sectionquiz.objects.all()
        return render(request,"list.html",{'st':st,'s':s})

模型.py

from django.db import models

# Create your models here.
class categoryquiz(models.Model):
    category=models.CharField(max_length=10)

    def __unicode__(self):
        return self.name
class sectionquiz(models.Model):
    var1=models.ForeignKey(categoryquiz)
    section=models.CharField(max_length=10)
    def __unicode__(self):
        return self.var.name

html

<html>
<head>
</head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

        $('#drop1').on('change', function() { 
            alert($("#drop1").val()); 
            $

});
});
</script>
<body>
category:<select id="drop1">
{% for i in st %}
<option value={{i.id}}>{{i.category}}</option>
{% endfor %}
</select>
section:<select id="drop2">
{% for i in s %}
<option value={{i.id}}>{{i.section}}</option>
{% endfor %}
</select>
<table id="tab1">
{% for i in s %}
<tr>
 <td> {{i.var1.category}}</td>
  <td>{{i.section}}</td>
  </tr>
{% endfor %}
</table>
<div id="div1">
</div>
</html>

在我的 html 文件中,我可以分别显示两个表的值。也是组合形式。我只想使用 jquery 显示它。当我选择类别时,相应的部分应显示在其他下拉列表或文本框中。

帮我继续..

4

0 回答 0