2013-12-17 144 views
1

我想从django中的模型中获取数据。我有一个代码,但有一个名称错误。 views.py 高清消息(请求):名称错误:未定义全局名称'loader'

from django import template 
    from django.template.loader import get_template 

    template_source_loaders = None 
     template = loader.get_template("news.html") 
     warning_list = [] 
     news_list = [] 
     blog_query = blogs.object.all() 

     for news_entry in blog_query: 
      news_list.append(news_entry) 
     #except Exception, e: 
      warning_list.append(e.message) 
     context = Context({ 
      'news_list':news_list, 
      'warnings' :warning_list, 
      }) 
     return HttpResponse(template.render(context)) 

模板: -

{%if warnings%} 
{%for warning in warnings%} 
    {{warning}} 
     {%endfor%} 
    {%endif%} 
    {%if news_list%} 
     {%for news in news_list%} 
      {{news.headline}} 
      {{news.date}} 
      {{news.content}} 
      <a href="{{news.author.website}}">{{news.author.author}}</a> 
     {%endfor%} 
    {%endif%} 

请帮我解决这个问题。

+2

你进口'get_template ''来自'loader'。这是基本的Python,所以我建议你在使用Django之前再阅读一下纯Python。 – Blender

+2

你现在表现得非常糟糕。回答你的问题后,你可以编辑标题以提出另一个不同的问题。如果这还不够糟糕,第二个问题的原因与第一个问题完全相同。我已经回滚了编辑并将投票结束。 –

回答

1

使用此代码

template = get_template("news.html") 

instaed的

template = loader.get_template("news.html") 

您的问题将是solved.Because你已经impotred功能get_template因此需要使用loader.get_template

+0

或者添加导入加载器:'from django.template import loader' – Guru