Passing a variable to display in django template

情到浓时终转凉″ 提交于 2019-12-10 12:25:19

问题


Hi I'm new to django and there's something I can't figure out. How exactly can I display a variable set in a different file?

choices.py:

testvar= 'This is a test variable'

views.py

from .choices import testvar

template.html

{{testvar}}

Is that it? The import seems to work properly but the string doesn't show. Thanks.


回答1:


Your view has to be something like:

from django.shortcuts import render

def index(request):
    testvar = 'value'
    return render(request, 'template.html', {'testvar': testvar})

You pass the dictionary that contains your values.




回答2:


No.

Just like the tutorial explains, you need to pass it as a context entry to the render() method of the template object.



来源:https://stackoverflow.com/questions/37893356/passing-a-variable-to-display-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!