社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  问与答

[精华] 一个网页显示一篇文章和可能存在的属于它的一个或多个图片,该怎么渲染?

243367782 • 10 年前 • 4252 次点击  

大家好,我第一发帖请多包含。

这是模板html: {% block title %}{{ title }}{% endblock %} {% block content %} <--h1>{{ title }}<--/h1> <table> <tr><td>Catelog:{{ catalog }} </td><td> Source:{{ source }} </td><td> Uploaded time:{{ upld_time }} </td><td> </tr></table> {% for u in urls %} <--img src="{{ u.img.url }}" height="260" width="260" /> {% endfor %} <p> {{ body|linebreaks }} </p> {% endblock %}

我从数据库读出一篇文章,可能含有图片:这是views:

from django import template from django.shortcuts import render_to_response from django.http import HttpResponse, Http404 from models import * from django.template import RequestContext

def test(request):

art = Article.objects.filter(id__exact=1)
imgs = Image.objects.filter(article=art)

if art:
    art = art.values()[0]
    print art
    return render_to_response('page.html',art,context_instance=RequestContext(request))
else:
    raise Http404

def tr(request):

art = Article.objects.filter(id__exact=1)
imgs = Image.objects.filter(article=art)
context = {'urls':imgs}
return render_to_response('test.html',context,context_instance=RequestContext(request))

请问我怎么样才能渲染这个模板呢?这里test函数和tr函数可以分别工作,如何把他们弄到一起渲染模板?请问这能做到吗?谢谢大家!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/349
 
4252 次点击  
文章 [ 3 ]  |  最新文章 10 年前
243367782
Reply   •   1 楼
243367782    10 年前

@BeginMan 成功了,我直接返回html内容用x.innerHTML = data 就行了,多谢多谢!

243367782
Reply   •   2 楼
243367782    10 年前

@BeginMan 谢谢提示。结果又遇到个问题,返回到浏览器的QuerySet不会处理了,转成Json也不会处理。

这是转成Json后的样子。 [{"pk": 1, "model": "astro.image", "fields": {"caption": " Thousands of people attend Friday prayers in Fallujah on Jan. 3.", "article": 1, "img": "images/folder_blue.ico", "credit": "MOHAMMED JALIL/EPA"}}, {"pk": 2, "model": "astro.image", "fields": {"caption": " Thousands of people attend Friday prayers in Fallujah on Jan. 3.", "article": 1, "img": "images/folder_blue_1.ico", "credit": "MOHAMMED JALIL/EPA"}}, {"pk": 3, "model": "astro.image", "fields": {"caption": "ds9-icon", "article": 1, "img": "images/ds9.ico", "credit": "MOHAMMED JALIL/EPA"}}]

里面一共有三个对象,分别包括图片说明,图片地址等内容,我该怎么把里面的内容分别展示在网页上啊?再教我一下好吗?

BeginMan
Reply   •   3 楼
BeginMan    10 年前

ajax.