【问题标题】:Django interaction with htmlDjango 与 html 的交互
【发布时间】:2021-03-03 03:50:46
【问题描述】:

您好,当我到达教程时,我正在学习如何使用 Django,其中人员使用此 {{}} 调用其他文件(views.py)的变量。 有人可以解释一下这个html是如何与views.oy文件交互的。 代码示例可能是这样的:

{% extends 'accounts/main.html'%}
{% load static %}
{% block content %}

<br>
<div class="row">
    <div class="col_md_6">
        <div class="card card-body">

            <form action="" method="POST">
                <!-- Esto es una manera de mandar datos de manera segura -->
                {% csrf_token %}
                {{formset}}
                <input type="submit" name="submit">
                
            </form>
        </div>
    </div>
</div>
{% endblock%}

为什么我可以像 {{formset}} 这样引用?

【问题讨论】:

    标签: python html django django-views django-templates


    【解决方案1】:

    在 Django 框架中,我们使用数据上下文来存储我们要发送模板的数据 (.html) 之后我们可以使用分隔符 {{}}

    访问该值

    例如: 视图.py

    def your_view_name(request):
        value = 12443
        context = {
            'template_var_name': value,
        }
        return render(request, 'your_template.html', context=context)
    

    在 your_template.html 中:

    您可以使用您在该上下文字典中定义的名称访问值

    <div>
        template_var_name is <strong>{{template_var_name}}</strong>
    </div>
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2020-10-29
        • 1970-01-01
        • 2021-07-14
        • 1970-01-01
        • 2014-08-30
        • 2011-07-09
        • 1970-01-01
        • 2017-01-11
        • 1970-01-01
        相关资源
        最近更新 更多