test development python django-5. Template templates

Foreword

is a static html language, which can not pass some dynamic parameters, that is a hard-coded html page. If you want to achieve in a fixed html style, passing different parameters, which can be used to pass django template parameters to solve.

Template Parameters

First create a new folder under templates hello application, the following directory hierarchy

└─helloworld
    │  db.sqlite3
    │  manage.py │ __init__.py │ ├─hello │ │ admin.py │ │ apps.py │ │ models.py │ │ tests.py │ │ views.py │ │ __init__.py │ │ │ ├─migrations │ │ │ __init__.py │ │ │ ├─templates │ │ yoyo.html │ │ __init__.py │ └─helloworld │ settings.py │ urls.py │ wsgi.py │ __init__.py

Yoyo.html create a new file, hello / templates / yoyo.html document reads as follows, template variables {{变量名称}}to represent

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>上海-悠悠</title> </head> <body> <h1>hello world! {{name}}同学</h1> </body> </html>

Add template in settings.py script inside path, modify TEMPLATES DIRS is in [BASE_DIR+"/hello/templates",]
front BASE_DIR values have been defined as the path of the current script:os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/hello/templates",],   # 默认为[] 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]

View the url

hello / views write a view function, as follows

from django.http import HttpResponse, Http404

# Create your views here.

def yoyo(request): context = {} context['name'] = '悠悠' return render(request, 'yoyo.html', context)

helloworld / urls.py add the access path

from django.conf.urls import url
from django.urls import re_path, path
from hello import views urlpatterns = [ path("yoyo/", views.yoyo), ]

Then enter the address in the browser: http://127.0.0.1:8000/yoyo/ you can visit

Django template tags

if / else tag

Conditional if statement, the end of the last endif

{% if condition1 %} ... display 1 {% elif condition2 %} ... display 2 {% else %} ... display 3 {% endif %}

for labels

Python for statement and the situation is similar loop syntax is for X in Y, Y is the sequence to loop over and X is the variable name used in each particular cycle.

Each cycle, the template will render {% for%} everything between {% endfor%} and.

<ul>
{% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} </ul>

ifequal / ifnotequal label

{% Ifequal%} tag compares two values, when they are equal, show all the values in {% ifequal%} and {% endifequal%} in.
The following example compares the template variables user and two currentuser:

{% ifequal user currentuser %} <h1>Welcome!</h1> {% endifequal %}

And the like {% if%}, {% ifequal%} supports an optional tag {% else%}

{% ifequal section 'sitenews' %} <h1>Site News</h1> {% else %} <h1>No News Here</h1> {% endifequal %}

Comment tags

# # Note Django using {}.

{# 这是一个注释 #}

Guess you like

Origin www.cnblogs.com/mashuqi/p/10972354.html