django:模板如何查询两个数据表

时间:2013-12-17 13:39:28

标签: python mysql django

使用 django :我有2个相同的数据库表需要同意页面显示,不知道如何运行它。

django views.py:

from django.shortcuts import render
from book.models import article, update
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render_to_response


# Create your views here.
def home(request):
    return HttpResponse("Hello world!")

def article_list(request, id):
    articles = article.objects.get(id = id)
    return render_to_response('article.html', {'article':articles})

def title_list(request, id):
    updates = update.objects.filter(bookid = id)
    return render_to_response('article.html', {'update':'updates'})

和urls.py:

from django.conf.urls import patterns, include, url
from book import views
#, article, update
from django.contrib import admin
admin.autodiscover()
from django.conf import settings

urlpatterns = patterns('',
    url(r'^templates/(?P<path>.*)$', 
        'django.views.static.serve',
        {'document_root': '/home/hugo/sobook/book/templates'}),
    url(r'^$', views.home, name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^b/(\d{1,9})/$', views.article_list, name='article_list'),
    url(r'^b/(\d{1,9})/$', views.title_list, name='title_list'),
    url(r'^admin/', include(admin.site.urls)),
    )

和models.py:

from django.db import models
from django.contrib import admin

class article(models.Model):
    name = models.CharField(max_length=20)
    #name = models.ManyToManyField(update)
    category = models.CharField(max_length=20)
    page_url = models.CharField(max_length=100)
    page_title = models.CharField(max_length=100)
    author = models.CharField(max_length=20)
    intro = models.CharField(max_length=500)
    img_url = models.CharField(max_length=100)
    state = models.CharField(max_length=10)
    siteid = models.CharField(max_length=2)
    sitename = models.CharField(max_length=20)
    time = models.CharField(max_length=20)

    def __unicode__(self):
        return self.name

class update(models.Model):
    siteid = models.CharField(max_length=2)
    bookid = models.CharField(max_length=50)
    name = models.CharField(max_length=20)
    #name = models.ManyToManyField(article)
    time = models.CharField(max_length=20)
    page_url = models.CharField(max_length=100)
    page_title = models.CharField(max_length=100)

和模板:article.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
       <link href="/templates/screen.css" rel="stylesheet" type="text/css" media="screen" />
    <title>{{article.name}}</title>
  </head>
  <body>
<h1>{{article.name}}</h1>
<ul><li><img src='{{article.img_url}}' width="100px"/></li>
<li>作者:{{article.author}} </li>
<li>简介: {{article.intro}}</li>
<li>入库时间:{{article.time|date:"F j, Y" }}</li>
<li>小说状态:{{article.state}}</li>
<li><a href="{{article.page_url}}" title="{{article.name}}">{{article.page_title}}</a></li>
</ul>
<div class="list">
<table border="1">
<tr>
  <th>最新章节</th>
  <th>来源</th>
  <th>更新时间:{{ title_list.page_title }}</th>
</tr>
{% for person in title_list %}
<tr>
  <td><a href="{{ person.page_url }}">{{ person.page_title }}</a></td>
  <td>{{ person.sitename }}</td>
  <td>{{ person.time }}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>

访问时:

  

本地主机/ B / 12 /

views.py title_list

没有显示查询?其他一切都正常。

1 个答案:

答案 0 :(得分:0)

观点中有一个错误。

from django.shortcuts import render
from book.models import article, update
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render_to_response


# Create your views here.
def home(request):
    return HttpResponse("Hello world!")

def article_list(request, id):
    articles = article.objects.get(id = id)
    return render_to_response('article.html', {'article':articles})

def title_list(request, id):
    updates = update.objects.filter(bookid = id)
    # You are passing the string 'updates' along instead of the queryset
    #return render_to_response('article.html', {'update':'updates'})
    return render_to_response('article.html', {'update':updates})

更多你的观点期待一个名为“title_list”的变量,但是这不是提供的,也许你的意思是“更新”。

在模板中,您将title_list称为更新实例和查询集。 您有{{ title_list.page_title }}{% for person in title_list %}这不太可能是您创建的实际对象,很可能是错误。

在URLS中,您也放弃了同一路径的两个视图。这根本不可能。每个URL都必须指向一个视图。您不能将多个视图分配到同一个网址。

url(r'^b/(\d{1,9})/$', views.article_list, name='article_list'),
url(r'^b/(\d{1,9})/$', views.title_list, name='title_list'),