Django pass variable into template

后端 未结 4 1871
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 15:01

Hi thank you for helping, I\'m poor in coding.

To point: I\'m doing a Django project that pass data form data-base to front-end; but right now i can\'t even pass an

4条回答
  •  没有蜡笔的小新
    2020-12-20 15:51

    You can try something like this

    views.py

    from django.template.response import TemplateResponse
    
    def testdex(requset, template_name="myapp/inculdes.html"):
        args = {}
        text = "hello world"
        args['mytext'] = text
        return TemplateResponse(request, template_name, args)
    

    inculdes.html

    {% extends "myapp/index.html" %}
    {% block includes %}
    {{ mytext }}
    {% endblock includes %}
    

    And make sure you have set path for templates in settings.py

提交回复
热议问题