Django:在多个地方使用块? [英] Django: Use block in multiple places?

查看:50
本文介绍了Django:在多个地方使用块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写的模板

{% extends 'base.html' %}
{% load static %}
{% block body %}
    <div class="row hide-on-mobile" style="height: 100vh">
        <div class="col-auto p-5">
            <img src="{% static 'logo.svg' %}" class="mb-5 hide-on-mobile" style="height: 84px"/>
            {% block form %} {% endblock %}
        </div>
        <div class="col bg-brown p-5">
            <div class="d-flex flex-column justify-content-between" style="height: 100%">
                <h1 class="text-blue mb-3">It's always Ups<br>and Downs</h1>
                <img src="{% static 'investing_illustration.svg' %}" class="auth-illustration"/>
            </div>
        </div>
    </div>
    <div class="hide-on-desktop" style="height: 100vh; position: relative">
        <div class="d-flex flex-column bg-brown h-100 p-3 align-items-start">
            <img src="{% static 'logo.svg' %}" class="mb-3 hide-on-desktop" style="height: 56px"/>
            <p class="text-blue mb-3">It's always Ups<br>and Downs</p>
            <img src="{% static 'investing_illustration.svg' %}" class="auth-illustration"/>
        </div>
        <div style="position: absolute; bottom: 0; z-index: 99; background: white; width: 100%">
           <div class="d-flex flex-column p-4">
               <p class="mb-4">Login</p>

               {% block form %} {% endblock %}
           </div>
        </div>
    </div>
{% endblock %}

我将在页面中继承此模板,如下所示:

I inherit this template in a page as follows:

{% extends 'auth_base.html' %}
{% block form %}
    <p class="mb-4">Login</p>

    <form class="pb-5">
        <input type="text" class="form-control" name="email" placeholder="Enter your email">
        <input type="password" class="form-control" name="password" placeholder="Enter your password">
        <a class="text-blue">Forgot Password?</a>
        <button type="submit" class="mt-3 btn btn-primary full-width">Login</button>
    </form>

   <div class="text-center">
        <span>New Here? <a class="text-blue">Create an account</a></span>
   </div>
{% endblock %}

form 块使用了两次..因为继承模板时,我只想编写一次..但要在同一页面的两个不同位置显示它.在这种情况下,我为移动设备和台式机编写了不同的布局,以显示用户实际上只能看到一次.

The form block is used two times..because when I inherit the template I want to write only once..but display it in two different places in the same page. In this case, I've written different layouts for mobile and desktop show the user actually sees only once.

但是,此代码不起作用,并出现以下错误:

However, this code doesn't work and gives the following error:

django.template.exceptions.TemplateSyntaxError: 'block' tag with name 'form' appears more than once

我该怎么做?

推荐答案

可以多次包含部分内容.部分操作的工作方式与块操作不同,而不是通过继承-而不是通过继承,因此您必须对模板代码进行一些更改.

It is possible to include partials multiple times. Partials work differently then blocks - not via inheritence, so you would have to change your template code a bit.

您需要为表单创建一个部分模板,该模板需要特定的上下文(可能是 form ),并且您将在主模板中两次包含该模板代码段:

You would need to create a partial template for your form which would expect a certain context (the form probably), and you would include that template snippet twice in your main template:

<div class="d-flex flex-column p-4">
    <p class="mb-4">Login</p>
    {% include "_partials/login_form.html" %}
</div>

_partials/login_form.html 基本上将包含您现在在 form块中的内容.

_partials/login_form.html would basically contain what you had now inside your form block.

https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#include

正如其他人所指出的-以前,已经解决了多个具有相同名称的块的问题: https://stackoverflow.com/a/6427336/8401179

as someone else pointed out - the question on multiple blocks with the same name has already been addressed before: https://stackoverflow.com/a/6427336/8401179

这篇关于Django:在多个地方使用块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆