models.py
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=200)
city = models.CharField(max_length=50)
country = models.CharField(max_length=30)
website = models.URLField()
class Book(models.Model):
title = models.CharField(max_length=50)
publicate_date = models.DateField()
publisher = models.ForeignKey(Publisher, null=True)
urls.py
urlpatterns = [
url('^otm/(\d*)$', views.otm_views),
]
views.py
def otm_views(request, pid):
publishers = Publishere.objects.all()
books = Book.objects.all()
if pid:
pid = int(pid)
if pid > 0:
books = Book.objects.filter(publisher_id=pid)
return render(request, 'otm.html', locals())
html/otm.html
<p>
<select id="selPub">
<option value="0">===请选择===</option>
{% for pub in publishers %}
<option value="{{pub.id}}"
{% if pub.id == pid %}
selected
{% endif %}
>
{{pub.name}}
</option>
{% endfor %}
</select>
</p>
<div>
<table border="1" width="300">
<tr>
<th>书名</th>
<th>出版时间</th>
</tr>
{% for b in books %}
<tr>
<td>{{ b.title }}</td>
<td>{{ b.publicate_date }}</td>
</tr>
{% endfor %}
</table>
</div>
javascript/otm.html
<script src="/static/js/jquery-1.11.3.js></script>
<script>
$(function(){
$("#selPub").change(function(){
location.href = "/otm/"+this.value;
});
});