From b88c4f4750a081608cea70ca6a6bb8e93317a5ea Mon Sep 17 00:00:00 2001 From: Saeid Javadi Date: Tue, 29 Oct 2024 20:15:24 +0330 Subject: [PATCH] Updating the Django template section and completing another section --- source/_posts/django.md | 99 ++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 22 deletions(-) diff --git a/source/_posts/django.md b/source/_posts/django.md index 5f48843..58a0229 100644 --- a/source/_posts/django.md +++ b/source/_posts/django.md @@ -110,8 +110,12 @@ Migrate: Will read the migrations files and create the actual database and table ## Project config {.cols-1} ### Configuration settings - - +##### Project base templates directory and Apps templates directory: + - create folder project/templates + - create folder appfolder/templates/appname +##### Create Static folder in base root and separate for apps: +- project_name\static\root +- app_name\static\app_name ```python # Add app to settings.py @@ -120,27 +124,18 @@ INSTALLED_APPS = [ 'app_name.apps.App_nameConfig', ] -# App templates folder -create folder appfolder/templates/appname - -# Project templates folder: -create folder projectname/templates - -# settings.py template config +# template directory config Project templates settings.py: TEMPLATES = [ { … 'DIRS': [BASE_DIR / 'templates'], … } -# Create Static folder: -project_name\static\ - -# Static folder (settings.py): +# Static folder: STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'static' ] STATIC_ROOT = 'static_root' -# Media folder (settings.py): +# Media folder: MEDIA_URL = '/media/' STATICFILES_DIRS = ( (BASE_DIR / 'static'), @@ -529,15 +524,20 @@ Templates are store in `project_folder/templates` or in your app_folder/te ```python +# Extend from another template +# can use the same parts of your HTML for different template {% extends 'base.html' %} -{% block content %} -{% endblock %} +# A part of the parent template that is defined and is replaced by a part in the child template +{% block contents %} +{% endblock contents %} + +# include template {% include 'partials/header.html' %} - # include template with One or More Parameters {% include 'body.html' with key1=value1 key2=value2 %} +# If statement in template {% if user.username = 'Mike' %}

Hello Admin

{% elif user.username = 'john' %} @@ -546,15 +546,20 @@ Templates are store in `project_folder/templates` or in your app_folder/te

Hello User

{% endif %} +# for loop in template {% for product in products %} -

product number: {{ forloop.counter0 }}

-

The product name is {{ product }}

+

row: + {{ forloop.counter }} # starting index 1 + {{ forloop.counter0 }} # starting index 0 +

+

The product name is {{ product.name }}

+

The product name is {{ product.price }}

{% endfor %} # Access to the variable in the template {{ var_name }} -# Template variables formatting +# Template variables formating {{ title | lower }} {{ blog.post | truncatwords:50 }} {{ order.date | date:"D M Y" }} @@ -577,6 +582,53 @@ Templates are store in `project_folder/templates` or in your app_folder/te

Hello {{ name }}!
{% endwith %} + +# Template translate text +{% load i18n %} +{% trans "This is the title." %} +# Use variable translate in the template +{% trans object.title %} + +# Define the list in the template + + +# Safely Pass Data to JavaScript in a Django Template: +#+ Use data attributes for simple values + + +#+ Separate script files: can use document.currentScript for separate script files + + +#+ Case conversion + +# Read it in JavaScript as fullName: +const data = document.currentScript.dataset; +const fullName = data.fullName; + +#+ Non-string types + +# parseInt() to convert this value from a string: +const data = document.currentScript.dataset; +const followerCount = parseInt(data.followerCount, 10); + +#+ There’s no limit: A ``` ### Custom Template Tags and Filters @@ -857,12 +909,15 @@ messages.error(request, 'Login error') # Message tags # debug, info, success, warning and error - -# Display flash in template +``` +```django +# Display flash messages in template {% if messages %} {% for message in messages %} {% message %} {% message.tags %} + {% endfor %} +{% endif %} ``` ## User Model