Include a view in a template

后端 未结 3 1128
感情败类
感情败类 2021-02-20 09:15

In django I have a view that fills in a template html file but inside the html template I want to include another view that uses a different html template like so:



        
3条回答
  •  伪装坚强ぢ
    2021-02-20 09:32

    Using your example and your answer to Brandon's response, this should work for you then:

    template.html

    {% block content %}
    Hey {{stuff}} {{stuff2}}!
    
    {{ other_content }}
    
    {% endblock content %}
    

    views.py

    from django.http import HttpResponse
    from django.template import Context, loader
    from django.template.loader import render_to_string
    
    
    def somepage(request): 
        other_content = render_to_string("templates/template1.html", {"name":"John Doe"})
        t = loader.get_template('templates/template.html')
        c = Context({
            'stuff': 'you',
            'stuff2': 'the rocksteady crew',
            'other_content': other_content,
        })
        return HttpResponse(t.render(c))
    

提交回复
热议问题