将从views.py传递的变量添加到模板中的静态文件中

问题描述 投票:0回答:1

我有4个pdf文件,其名称如“lesson-1,lesson-2,lesson-3”等。我希望每个文件都能在访问每个文件的路径时进行渲染。如何将来自views.py的变量传递到模板的静态链接?

我试过了

{% static 'lectii/lectie-' | add:{lectie}.pdf %}
{% static 'lectii/lectie-{lectie}.pdf %}

没有人工作。

这是模板:

{% extends 'base.html' %}
{% load static %}
{% block content %}
    </div>
    <div id="middle-section" class="container-fluid container-fluid-margin">
        <div class="row content-block">
            <div class="col-md-12">
                <embed src="{% static 'lectii/lectie-{lectie}.pdf %}" style="width: 100%; height: 100vh;"></embed>    
            </div>
        </div>
        <div class="row content-block">
            <iframe width="100%" height="750" src="https://www.youtube.com/embed/I6dQXpJKlPk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        </div>
    </div>
{% endblock %}

views.朋友

from django.shortcuts import render
from django.shortcuts import get_object_or_404
from .models import Lectie

def lectii(req):
    return render(req, '../templates/pagini/lectii-selector.html')

def lectie(req, lectie_id):
    lectie2 = get_object_or_404(Lectie, pk=lectie_id)
    context = {
        'lectie': lectie2
    }
    return render(req, '../templates/pagini/lectii.html', context)

(课程=课程)

那么如何将变量包含在该静态路径中呢?谢谢。

python html django
1个回答
1
投票

在您的情况下,最简单的方法是:

src="{% static 'lectii/lectie' %}-{{ lectie }}.pdf"
© www.soinside.com 2019 - 2024. All rights reserved.