名称错误:未定义全局名称“loader”

时间:2013-12-17 08:22:35

标签: python django

我想从django中的模型中获取数据。我有一个代码,但有一个名称错误。 views.py def news(请求):

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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

使用此代码

    template = get_template("news.html") 

而不是

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

你的问题将得到解决。因为你已经导入了函数get_template所以需要使用loader.get_template

相关问题