【发布时间】:2017-09-22 20:17:56
【问题描述】:
查看.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Albums
from django.template import loader
def index(request):
all_albums = Albums.objects.all()
template = loader.get_template('Ganaana/index.html')
context = {
'all_albums':all_albums,
}
return HttpResponse(template.render(context,request))
def define(request,Albums_id):
return HttpResponse("<h1>Your Id is "+str(Albums_id)+"</h1>");
index.html
<html>
<ul>
<% for albums in all_albums %>
<li><a href="/music/{{albums.id}}/">{{albums.artist}}</a></li>
<% endfor %>
</ul>
</html>
代码输出:
<% for albums in all_albums %>
albums.artist
<% endfor %>
我不知道我使用模板文件夹并将数据放入其中的错误我不明白我正确导入类的问题..我不明白?
【问题讨论】:
-
我们也不明白,因为您根本没有告诉我们您在使用此代码时遇到了什么问题。
-
另外,Python 代码在行尾不使用分号,缩进很重要。
-
@DanielRoseman 先生,我的输出在网页上不正确我已经调整了代码请查看它。也许现在你可以理解我的问题了
标签: django django-templates django-views