Skip to main content
 首页 » 编程设计

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

2024年11月01日9telwanggs

<分区>

我想从 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%} 

请帮我解决这个问题。