Templates - Relative URLs+ Inheritance + Filter 본문
Relative URLs with Templates
-------
# project/urls.py
from django.contrib import admin
from django.urls import path, include
from basic_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('basic_app/', include('basic_app.urls'))
]
-------
# app/urls.py
from django.urls import path
from basic_app import views
# TEMPLATE TAGGING
app_name = 'basic_app'
urlpatterns = [
path('relative/', views.relative, name='relative'),
path('other/',views.other, name='other'),
]
-------
# app/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'basic_app/index.html')
def other(request):
return render(request,'basic_app/other.html')
def relative(request):
return render(request, 'basic_app/relative_url_templates.html')
-------
# templates/app/relative_page.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Welcome to relative url</h1>
<!-- <a href="basic_app/other.html">TheOtherPage</a> -->
<!-- basic_app/urls.py -> Need to match with app_name -->
<a href="{% url 'basic_app:other'%}">THE OTHER PAGE</a>
<!-- Connecting to admin page -->
<a href="{% url 'admin:index' %}">LINK TO ADMIN</a>
</body>
</html>
---------------------------------------------
Inheritance
Main steps for inheritance
- Find the repetitive parts of your project
- Create a base template of them
- Set the tags in the base template
- Extend and call those tags anywhere
# templates/app/base.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<-- Bootstrap CDN -->
</head>
<body>
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<ul class="nav navbar-nav">
<li><a class="navbar-brand" href="{% url 'index'%}">BRAND</a></li>
<li><a class="navbar-link" href="{% url 'admin:index' %}">Admin</a></li>
<li><a class="navbar-link" href="{% url 'basic_app:other'%}">Other</a></li>
</ul>
</div>
</nav>
<div class="jumbotron">
{% block body_block %}
<!-- Anything outside of this will be inherited if you extend! -->
{% endblock %}
</div>
</body>
</html>
-------------
# index.html
<!DOCTYPE html>
{% extends "basic_app/base.html" %}
{% block body_block %}
<h1>Welcome to Index</h1>
<h2>This is index.html page</h2>
{% endblock %}
-------------
# other.html
<!DOCTYPE html>
{% extends "basic_app/base.html" %}
{% block body_block %}
<h1>Welcome to Other</h1>
<h2>This is an example of template inheritance!</h2>
{% endblock %}
---------------------------------------------
Filter
# app/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
context_dict = {'text':'hello world', 'number':100}
return render(request,'basic_app/index.html',context_dict)
def other(request):
return render(request,'basic_app/other.html')
def relative(request):
return render(request, 'basic_app/relative_url_templates.html')
---------
# templates/index.html
<!DOCTYPE html>
{% extends "basic_app/base.html" %}
{% block body_block %}
<h1>Welcome to Index</h1>
<h2>This is index.html page</h2><br>
<h3>{{ text|upper }}</h3>
<p>{{ number|add:"99" }}</p>
{% endblock %}
-----------------------------------
Custom Filter
# app/templatetags/__init__.py
# app/templatetags/my_extra.py
from django import template
register = template.Library()
def cut(value, arg):
"""
This cuts out all values of "arg" from the string!
"""
return value.replace(arg,'')
# (template_tag, function)
register.filter('cut',cut)
----------
# templates/app/index.html
<!DOCTYPE html>
{% load my_extra %}
{% extends "basic_app/base.html" %}
{% block body_block %}
<h1>Welcome to Index</h1>
<h2>This is index.html page</h2><br>
<h3>{{ text|upper }}</h3>
<h3>{{ text|cut:'hello' }}</h3>
<p>{{ number|add:"99" }}</p>
{% endblock %}
-------------
# app/templatetags/my_extra.py
from django import template
register = template.Library()
# Using Decorator
@register.filter(name='cut')
def cut(value, arg):
"""
This cuts out all values of "arg" from the string!
"""
return value.replace(arg,'')
'개발 > Django' 카테고리의 다른 글
Django Deployment (0) | 2020.05.13 |
---|---|
Login / Logout pages Implementation (0) | 2020.05.09 |
장고 공부법 (0) | 2020.05.07 |
Data List on the web page (0) | 2020.05.05 |
Models - Templates - Views (MTV) (0) | 2020.05.05 |