banner



Is A Directory: '/templates' Django

Author avatar

Introduction to Django Templates

Gaurav Singhal

Introduction

In the Django MTV model, the template is the presentation layer. This layer interacts with the user, sends the requests to the views, and responds to the user.

Django templates deal with HTML/CSS/JS. All rendering is handled in templates. Other than the basic constructs of HTML/CSS/JS, Django comes with templating language to render dynamic information in the form of templates. In this guide, yous will acquire about the Django templates and the language constructs that are necessary to build a Django application.

Why Django Templates

Django template is a text document or Python string marked up that uses Django template language. Django keeps the logic and lawmaking separate from the pattern. It is essential to know that Django templates practise non incorporate the embedded code of Python into HTML.

Django templates follow the DRY (do non repeat yourself) design principle, which requires developers to not repeat while designing a Django application. Django templates employ several other notions, such every bit template inheritance, tags, variables, filters, comments, etc.

Template Configuration

To configure the Django template system, get to the settings.py file and update the DIRS to the path of the templates binder. More often than not, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.

                                      1                    import                                          os                                        2                    3                    TEMPLATES                                        =                                                            [                                                            4                                                            {                                                            5                                                            'BACKEND'                    :                                                            'django.template.backends.django.DjangoTemplates'                    ,                                                            half dozen                                                            'DIRS'                    :                                                            [                    os                    .                    path                    .                    join                    (                    BASE_DIR                    ,                    'templates'                    )                    ]                    ,                                                            7                                                            'APP_DIRS'                    :                                                            True                    ,                                                            eight                                                            'OPTIONS'                    :                                                            {                                                            nine                                                            'context_processors'                    :                                                            [                                                            10                                                            'django.template.context_processors.debug'                    ,                                                            eleven                                                            'django.template.context_processors.request'                    ,                                                            12                                                            'django.contrib.auth.context_processors.auth'                    ,                                                            13                                                            'django.contrib.messages.context_processors.messages'                    ,                                                            xiv                                                            ]                    ,                                                            15                                                            }                    ,                                                            16                                                            }                    ,                                                            17                                        ]                                  

python

Alternatively, yous tin maintain a template binder for each app separately. If you are maintaining the template for every app separately, y'all don't need to update the DIRS in settings.py . Make sure that your app is added in INSTALLED_APPS in settings.py .

The Django Template Language

In the Django template language, the syntax involves 4 major constructs.

1. Variables

Variables output the value from the context. The context is generally a dict -like object (mapping variable name with its respective value) passed from views.

                                      1                    {{                                                            variable_name                                                            }}                                  

django

For instance, if you render the context set up equally shown below ...

                                      ane                    from                                          django                    .                    shortcuts                                        import                                          render                                        2                    3                                        def                                                            my_view                    (                    asking                    )                    :                                                            4                                          context                                        =                                                            {                                                            v                                                            "author"                    :                                                            "Gaurav Singhal"                    ,                                                            6                                                            "website"                    :                                                            {                                                            vii                                                            "domain"                    :                                                            "https://pluralsight.com"                    ,                                                            8                                                            "views"                    :                                                            200                                                            ix                                                            }                    ,                                                            x                                                            "odds"                    :                                                            [                    1                    ,                                                            3                    ,                                                            five                    ]                                                            11                                                            }                                                            12                                                            render                                          render                    (                    request                    ,                                                            "index.html"                    ,                                          context                    )                                  

python

... yous tin can access it in template alphabetize.html as :

                                      1                    {{                    author                    }}                                                            2                                        {{                    website                    .                    domain                    }}                                                            3                                        {{                    website                    .                    views                    }}                                  

django

ii. Tags

Tags give logic to the template linguistic communication to render the content logically.

                                      1                    {%                                                            if                                                            user                    .                    is_authenticated                                                            %}                    Howdy,                                        {{                                                            user                    .                    username                                                            }}                    , How are you                                        {%                                                            endif                                                            %}                                  

django

There are several tags which are used in templates Some of them are:

  •                                                   ane                          {%                                                                              if                                                                              condition                                                                              %}                                                                              2                          renders if torso                          3                                                    {%                                                                              else                                                                              %}                                                                              4                          renders else torso                          5                                                    {%                                                                              endif                                                                              %}                                              

    django

  •                                                   ane                          {%                                                                              for                                                                              odd                                                                              in                                                                              odds                                                                              %}                                                                              2                                                    {{                          odd                          }}                                                                              three                                                    {%                                                                              endfor                                                                              %}                                              

    django

This is used to load a template and render it to the current context. It is very useful for creating several components separately, such as navbar, footer, etc., then including them in several templates.

                                      one                    {%                                                            include                                                            template_name                                                            %}                                  

django

    This defines the block to be overridden past the kid template. You lot will get a improve idea of blocks when we discuss template inheritance .

                                          one                    {%                                                            block                                                            content                                                            %}                                                            2                                        {%                                                            endblock                                                            %}                                  

    django

    You can read more nigh built-in tags in the Django docs.

    3. Filters

    The filter is used to transform the value of the variable and tag statement.

                                          1                    {{                                                            my_variable                                                            |                                                            filter                                                            }}                                  

    django

                                          1                    {{                                                            my_date                    |                    date                    :                    "Y-one thousand-d"                                                            }}                                  

    django

    You can read more about filter and several built-in filters in the Django docs.

    four. Comments

    Y'all can easily add together a comment in Django templates:

                                          one                    {# Comment here. Information technology won't exist rendered #}                                  

    django

    Template Inheritance

    I of the most powerful parts of the Django template engine is template inheritance. This allows y'all to build a base skeleton template that contains all the base/common elements and defines blocks in a child/derived template later on extending the base template. It is easier to sympathise in do. Let's kickoff create a base.html .

                                          i                    <                    html                    >                                                            ii                                                            <                    head                    >                                                            3                                                            <                    title                    >                                                            four                                                            {%                                                            block                                                            title                                                            %}                    Base title                    {%                                                            endblock                                                            %}                                                            5                                                            </                    title                    >                                                            6                                                            {%                                                            comment                                                            %}                                          This Bootsrap css is added to all pages                                        {%                                                            endcomment                                                            %}                                                            7                                                            <                    link                                                            rel                    =                    "                    stylesheet                    "                                                            href                                                            =                                                            "                    https://cdn.jsdelivr.net/npm/[electronic mail protected]/dist/css/bootstrap.min.css                    "                    >                                                            8                                                            </                    head                    >                                                            9                                                            <                    body                    >                                                            10                                                            <                    p                    >                                                            11                    Body content which remains in all the pages                    12                                                            </                    p                    >                                                            13                                                            {%                                                            block                                                            content                                                            %}                                                            14                    Torso content                    fifteen                                                            {%                                                            endblock                                                            %}                                                            16                                                            </                    body                    >                                                            17                                        </                    html                    >                                  

    django

    Now create a child template and salvage information technology as index.html .

                                          1                    {%                                                            extends                                                            'base of operations.html'                                                            %}                                                            2                    iii                                        {%                                                            block                                                            title                                                            %}                                                            4This is alphabetize championship                    v                                        {%                                                            endblock                                                            title                                                            %}                                                            vi                    7                                        {%                                                            block                                                            content                                                            %}                                                            8Main content of index.html                    ix                                        {%                                                            endblock                                                            content                                                            %}                                  

    django

    At present when y'all render the alphabetize.html from the views, yous will get the following output:

                                          1                    Trunk content which remains in all the pages                                        2                    iiiPrimary content of index.html                                  

    Conclusion

    A Django template deals with passing data from a Django view to the browser and is considered the basic building block of a dynamic Django application. Y'all can refer to the Django documentation to learn Django template in depth.

    If you take whatsoever questions, feel free to reach out to me at Codealphabet.

    Is A Directory: '/templates' Django,

    Source: https://www.pluralsight.com/guides/introduction-to-django-templates

    Posted by: bidwellbitheirstake.blogspot.com

    0 Response to "Is A Directory: '/templates' Django"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel