2 つのテーブルがあり、外部キーを使用して関連付けています。jqueryを使ってテーブルの値を表示したい。
私のviews.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})
models.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ファイルでは、2つのテーブルの値を別々に表示できます。こちらも合体形態。jqueryで表示したいだけです。カテゴリを選択すると、対応するセクションが他のドロップダウンまたはテキスト ボックスに表示されます。
続行するのを手伝ってください..