python - 名称错误 : global name 'loader' is not defined

标签 python django

<分区>

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

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

请帮我解决这个问题。

最佳答案

使用此代码

    template = get_template("news.html") 

取代了

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

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

关于python - 名称错误 : global name 'loader' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20629184/

相关文章:

python - scrapy 有没有办法在不使用 -o -t 参数的情况下打印 json 文件

Python:如何将不同数据帧中具有相同名称的列彼此相邻放置?

django - 在 Django 中扩展 social_auth 用户模型

python - 如何在Python Django中从数据库表中获取相关类别产品?

python - celery 不工作 : Cannot connect to amqp://guest:**@127. 0.0.1:5672//

python - 如何使用 Nginx 提供 Flask 静态文件?

python - 使用python调整两个字符串之间的1个空格

python - Django - 为测试目的填充数据库

python - 带有朴素贝叶斯分类器的 n-gram

django - 类型错误 : context must be a dict rather than HttpResponseRedirect