问题
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