2017-09-22 37 views
-1

View.py到html无法将数据从模型转移使用模板

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 %> 

我不知道是什么错误我用模板文件夹,并把它的数据我不明白什么问题,我正确地导入类..我不明白?

+1

我们不明白,要么,因为你没有在所有的告诉我们,要使用此代码遇到什么问题。 –

+0

另外,Python代码在行尾没有使用分号,并且缩进很重要。 –

+0

@DanielRoseman先生我的输出是不正确的网页我已经调整了代码请检查它。也许现在你可以理解我的问题 –

回答

1

只要使用此:

from django.shortcuts import render 

def index(request): 
    return render(request, "Ganaana/index.html", {"all_albums": Albums.objects.all()}) 
+0

它的工作谢谢你先生.. –

相关问题