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%}
請幫我解決這個問題。
你進口'get_template ''來自'loader'。這是基本的Python,所以我建議你在使用Django之前再閱讀一下純Python。 – Blender
你現在表現得非常糟糕。回答你的問題後,你可以編輯標題以提出另一個不同的問題。如果這還不夠糟糕,第二個問題的原因與第一個問題完全相同。我已經回滾了編輯並將投票結束。 –