gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:55:08 24 4
gpt4 key购买 nike

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

django View .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>

访问时:

localhost/b/12/

views.py title_list

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

最佳答案

View 中有一个错误。

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})

此外,您的 View 需要一个名为“title_list”的变量,但是未提供该变量,也许您的意思是“更新”。

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

在 URLS 中,您还放弃了同一路径的两个 View 。这根本不可能。每个 URL 必须指向一个 View 。您不能将多个 View 分配给同一个 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'),

关于python - django:模板如何查询两个数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20635701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com