我已经创建了一个我想要使用的自定义标记,但Django似乎无法找到它。我的templatetags
目录设置如下:
pygmentize.py
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from django import template
from pygments.formatters.other import NullFormatter
register = template.Library()
@register.tag(name='code')
def do_code(parser,token):
code = token.split_contents()[-1]
nodelist = parser.parse(('endcode',))
parser.delete_first_token()
return CodeNode(code,nodelist)
class CodeNode(template.Node):
def __init__(self,lang,code):
self.lang = lang
self.nodelist = code
def render(self,context):
code = self.nodelist.render(context)
lexer = get_lexer_by_name('python')
return highlight(code,lexer,NullFormatter())
我正在尝试使用此标记在gameprofile.html
中呈现代码。
gameprofile.html
(% load pygmentize %}
{% block content %}
<title>{% block title %} | {{ game.title }}{% endblock %}</title>
<div id="gamecodecontainer">
{% code %}
{{game.code}}
{% endcode %}
</div>
{% endblock content %}
当我导航到gameprofile.html
时,出现错误:
第23行的无效广告代码:&#39;代码&#39;,预期&#39; endblock&#39;。您是否忘记注册或加载此标记?
答案 0 :(得分:10)
{% load games_tags %}
在顶部而不是pygmentize?
答案 1 :(得分:6)
错误在此行:(% load pygmentize %}
,无效标记。
将其更改为{% load pygmentize %}
答案 2 :(得分:2)
我有同样的问题,这是我如何解决它。在this very excellent Django tutorial的第一部分之后,我做了以下内容:
python manage.py startapp new_app
settings.py
文件,将以下内容添加到INSTALLED_APPS
列表中:'new_app',
new_app
的{{1}}包中。new_app_tags
之后:{% extends 'base_template_name.html' %}
{% load new_app_tags %}
模块文件中,创建自定义模板标记(见下文)。new_app_tags
上述第5步的例子:
{% multiply_by_two | "5.0" %}
答案 3 :(得分:2)
遇到了同样的问题,但我刚刚从我的扩展模板中添加了 #!/bin/sh
# zenity-select.sh
# A zenity-based .sh script (dash, bash, zsh - compatible) for
# manually marking files and folders in a source directory (recursive)
# and copying them to another directory (destination)
GET_ROOT_FOLDER () {
eval $1=\'$(zenity --file-selection --directory .)\'
}
GET_FILES_FOLDERS_OR_TEMPLATE () {
eval $1=\'$(zenity --list --column Selection --column "Files/Folders" TRUE "Files or Folders" FALSE "TEMPLATE" --radiolist)\'
}
GET_FILES_OR_FOLDERS () {
eval $1=\'$(zenity --list --column Selection --column "Files/Folders" TRUE "Files" FALSE "Folders" --radiolist)\'
}
GET_FILES () {
eval ROOT_FOLDER="\"\$$1\""
eval $2=\'$(zenity --file-selection --multiple --filename "${ROOT_FOLDER}/" --separator "/////")\'
}
GET_FOLDERS () {
eval ROOT_FOLDER="\"\$$1\""
eval $2=\'$(zenity --file-selection --multiple --directory --filename "${ROOT_FOLDER}/" --separator "/////")\'
}
GET_DESTINATION_FOLDER () {
eval $1=\'$(zenity --file-selection --directory .)\'
}
ProcGenerateMarkedArray () {
#Proceed to comparing files:
for i in $(seq 1 $count2); do
eval markings_$i="0"
done
for i in $(seq 1 $count2); do
eval current_file_i="\"\$file_or_folder_to_copy2_$i\""
eval current_marking_i="\"\$markings_$i\""
if [ "$current_marking_i" = "0" ]; then
for j in $(seq 1 $count2); do
eval current_marking_j="\"\$markings_$j\""
if [ ! "$i" = "$j" -a "$current_marking_j" = "0" ]; then
eval current_file_j="\"\$file_or_folder_to_copy2_$j\""
if [ ! "${current_file_i#$current_file_j"/"}" = "$current_file_i" ]; then
eval markings_$i="1"
break
elif [ ! "${current_file_j#$current_file_i"/"}" = "$current_file_j" ]; then
eval markings_$j="1"
fi
fi
done
fi
done
#Rebuild files array:
count3=0
for i in $(seq 1 $count2); do
eval current_marking="\"\$markings_$i\""
if [ "$current_marking" = "0" ]; then
count3=$((count3 + 1))
eval file_or_folder_to_copy3_$count3="\"\$file_or_folder_to_copy2_$i\""
fi
done
}
ProcExtractFoldersFromPath () {
eval file_path="\"\$$1\""
file_path=${file_path#"/"}
first_path_component="${file_path%%"/"*}"
remaining_file_path="${file_path#*"/"}"
folders_count=0
while [ ! "$first_path_component" = "$remaining_file_path" ]; do
folders_count=$((folders_count + 1))
eval $2_$folders_count="\"\$first_path_component\""
first_path_component="${remaining_file_path%%"/"*}"
remaining_file_path="${remaining_file_path#*"/"}"
done
#Treat last path component separately
folders_count=$((folders_count + 1))
eval $2_$folders_count="\"\$first_path_component\""
eval $3=$folders_count
}
trap1 () {
CleanUp
printf "\nAborted.\n"
}
CleanUp () {
#Restore "INTERRUPT" (CTRL-C) and "TERMINAL STOP" (CTRL-Z) signals:
trap - INT
trap - TSTP
#Restore initial IFS:
#IFS=$old_IFS
unset IFS
#Restore initial directory:
cd "$initial_dir"
}
TEMPLATE_FILE_DIR_PATH='.'
TEMPLATE_FILE_NAME='Template_zenity_select.txt'
cd "$TEMPLATE_FILE_DIR_PATH"
TEMPLATE_FILE_DIR_PATH="$PWD"
#If default template file is in $HOME folder, then set its DIRECTORY PATH as: $HOME folder PATH:
if [ ! "${TEMPLATE_FILE_DIR_PATH#$HOME}" = "$TEMPLATE_FILE_DIR_PATH" ]; then
TEMPLATE_FILE_DIR_PATH="$HOME"
fi
TEMPLATE_FILE_PATH="$TEMPLATE_FILE_DIR_PATH/$TEMPLATE_FILE_NAME"
initial_dir="$PWD"
old_IFS="$IFS" #Store initial IFS value
IFS="
"
#Trap "INTERRUPT" (CTRL-C) and "TERMINAL STOP" (CTRL-Z) signals:
trap 'trap1' INT
trap 'trap1' TSTP
main_count="1"
error1="false"
error2="false"
error3="false"
{ zenity --help >/dev/null 2>/dev/null; } || { error1="true"; }
{ uniq --help >/dev/null 2>/dev/null; } || { error2="true"; }
{ cp --help >/dev/null 2>/dev/null; } || { error3="true"; }
{ find --help >/dev/null 2>/dev/null; } || { error4="true"; }
if [ "$error1" = "true" -o "$error2" = "true" -o "$error3" = "true" ]; then
{
printf "\n"
if [ "$error1" = "true" ]; then printf '%s\n' "ERROR: Could not run \"zenity\" (necessary in order for this script to function correctly)!"; fi
if [ "$error2" = "true" ]; then printf '%s\n' "ERROR: Could not run \"uniq\" (necessary in order for this script to function correctly)"; fi
if [ "$error3" = "true" ]; then printf '%s\n' "ERROR: Could not run \"cp\" (necessary in order for this script to function correctly)"; fi
if [ "$error4" = "true" ]; then printf '%s\n' "ERROR: Could not run \"find\" (necessary in order for this script to function correctly)"; fi
printf "\n"
}>/dev/stderr
CleanUp; exit 1
fi
while [ ! "$main_count" = "2" ]; do
GET_FILES_FOLDERS_OR_TEMPLATE files_folders_or_template
if [ -z "$files_folders_or_template" ]; then CleanUp; exit 1; fi
if [ "$files_folders_or_template" = "Files or Folders" ]; then
SELECT_ROOT_FOLDER="$(zenity --list --column Selection --column "SELECT ROOT FOLDER" TRUE "Select ROOT folder" --radiolist)"
if [ -z "$SELECT_ROOT_FOLDER" ]; then continue; fi
GET_ROOT_FOLDER ROOT_FOLDER
if [ -z "$ROOT_FOLDER" ]; then continue; fi
if [ -z "$files_or_folders" ]; then
GET_FILES_OR_FOLDERS files_or_folders
if [ -z "$files_or_folders" ]; then exit; fi
else
GET_FILES_OR_FOLDERS files_or_folders
if [ -z "$files_or_folders" ]; then continue; fi
fi
files_folders_selection="temp"
current_file_or_folder="temp"
count=0
error="false"
while [ -n "$files_or_folders" -a -n "$files_folders_selection" ]; do
if [ "$files_or_folders" = "Files" ]; then
GET_FILES ROOT_FOLDER files_folders_selection
elif [ "$files_or_folders" = "Folders" ]; then
GET_FOLDERS ROOT_FOLDER files_folders_selection
fi
while [ ! "$current_file_or_folder" = "$files_folders_selection" ]; do
current_file_or_folder="${files_folders_selection%%"/////"*}"
files_folders_selection="${files_folders_selection#*"/////"}"
current_file_or_folder_relative_path="${current_file_or_folder#$ROOT_FOLDER"/"}"
if [ "$current_file_or_folder_relative_path" = "$current_file_or_folder" ]; then #If "$current_file_or_folder" does not start with "$ROOT_FOLDER":
error="true"
break
else
count=$((count+1))
eval file_or_folder_to_copy1_$count=\"\$current_file_or_folder_relative_path\"
fi
done
if [ "$error" = "true" ]; then
zenity --warning --width=400 --height=200 --text "ERROR: Last selection parent folder is not \"$ROOT_FOLDER\"! Please try again:"
fi
if [ -n "$files_folders_selection" ]; then
GET_FILES_OR_FOLDERS files_or_folders
if [ "$error" = "true" -a -z "$files_or_folders" ]; then CleanUp; exit 1; fi
fi
done
count2=0
for line in $(\
for i in $(seq 1 $count); do\
eval current_file_or_folder="\"\$file_or_folder_to_copy1_$i\""
printf '%s\n' "$current_file_or_folder";\
done|uniq -u;\
); do
count2=$((count2+1))
#printf '%s\n' "$line"
eval file_or_folder_to_copy2_$count2=\"\$line\"
done
ProcGenerateMarkedArray
answer=""
zenity --question --text "Save this selection to template file?" --no-wrap --ok-label "Yes" --cancel-label "No"
error_code="$?"; if [ "$error_code" = "0" ]; then answer="Yes"; fi
if [ "$answer" = "Yes" ]; then
answer=""
answer="$(zenity --list --column Selection --column "" TRUE "Use default template file path defined in the script" FALSE "Define a template file path" --radiolist --width 480 --height 180)"
if [ "$answer" = "Define a template file path" ]; then
zenity --info --width=400 --height=200 --text "Please provide the directory where you want to save the template file:"
TEMPLATE_FILE_DIR_PATH="$(zenity --file-selection --directory ".")"
if [ -z "$TEMPLATE_FILE_DIR_PATH" ]; then CleanUp; exit 1; fi
TEMPLATE_FILE_PATH="$TEMPLATE_FILE_DIR_PATH/$TEMPLATE_FILE_NAME"
answer=""
if [ -n "$TEMPLATE_FILE_PATH" ]; then
zenity --question --text "Are you sure you want to continue? If Yes:\n\nWARNING: $TEMPLATE_FILE_PATH will be overwritten!" --no-wrap --ok-label "Yes" --cancel-label "No"
error_code="$?"; if [ "$error_code" = "0" ]; then answer="Yes"; fi
fi
elif [ "$answer" = "Use default template file path defined in the script" -o "$answer" = "Yes" ]; then
cat "/dev/null">"$TEMPLATE_FILE_PATH"
for i in $(seq 1 $count3); do
eval current_file_or_folder="\"\$file_or_folder_to_copy3_$i\""
printf '%s\n' "$current_file_or_folder">>"$TEMPLATE_FILE_PATH"
done
else
CleanUp; exit 1
fi
fi
SELECT_DESTINATION_FOLDER="$(zenity --list --column Selection --column "SELECT DESTINATION FOLDER" TRUE "Select DESTINATION folder" --radiolist --width 400 --height 200)"
if [ -z "$SELECT_DESTINATION_FOLDER" ]; then CleanUp; exit 1; fi
GET_DESTINATION_FOLDER DESTINATION_FOLDER
if [ -z "$DESTINATION_FOLDER" ]; then CleanUp; exit 1; fi
elif [ "$files_folders_or_template" = "TEMPLATE" ]; then
answer=""
answer="$(zenity --list --column Selection --column "" TRUE "Use the default template file path defined in the script" FALSE "Use a user defined template file path" --radiolist --width 510 --height 180)"
if [ "$answer" = "Use a user defined template file path" ]; then
zenity --info --width=400 --height=200 --text "Please provide the template file path:"
TEMPLATE_FILE_PATH="$(zenity --file-selection --filename ".")"
if [ -z "$TEMPLATE_FILE_PATH" ]; then continue; fi
elif [ "$answer" = "Use the default template file path defined in the script" ]; then
TEMPLATE_FILE_PATH="$TEMPLATE_FILE_DIR_PATH/$TEMPLATE_FILE_NAME"
else
continue
fi
if [ -e "$TEMPLATE_FILE_PATH" -a -f "$TEMPLATE_FILE_PATH" ]; then
count2=0
for line in $(cat "$TEMPLATE_FILE_PATH"); do
count2=$((count2+1))
eval file_or_folder_to_copy2_$count2=\"\$line\"
done;
ProcGenerateMarkedArray
SELECT_ROOT_FOLDER="$(zenity --list --column Selection --column "SELECT ROOT FOLDER" TRUE "Select ROOT folder" --radiolist)"
if [ -z "$SELECT_ROOT_FOLDER" ]; then continue; fi
GET_ROOT_FOLDER ROOT_FOLDER
if [ -z "$ROOT_FOLDER" ]; then continue; fi
SELECT_DESTINATION_FOLDER="$(zenity --list --column Selection --column "SELECT DESTINATION FOLDER" TRUE "Select DESTINATION folder" --radiolist --width 400 --height 200)"
if [ -z "$SELECT_DESTINATION_FOLDER" ]; then CleanUp; exit 1; fi
GET_DESTINATION_FOLDER DESTINATION_FOLDER
if [ -z "$DESTINATION_FOLDER" ]; then CleanUp; exit 1; fi
else
zenity --warning --width=400 --height=200 --text "ERROR: Default template defined in the script does not exist! Please create one, by not using a template, first."
continue
fi
fi
if [ -n "$count3" -a ! "$count3" = 0 ]; then
#Proceed to copying files:
error="false"
count5=0
#Get ROOT_FOLDER_NAME
ROOT_FOLDER_NAME="${ROOT_FOLDER##*"/"}"
while [ ! -z "$DESTINATION_FOLDER" ]; do
if [ -e "$DESTINATION_FOLDER/$ROOT_FOLDER_NAME" ]; then
zenity --warning --width=400 --height=200 --text "ERROR: The destination folder name \"$ROOT_FOLDER_NAME\" already exists in "$DESTINATION_FOLDER"! Please try again:"
SELECT_DESTINATION_FOLDER="$(zenity --list --column Selection --column "SELECT DESTINATION FOLDER" TRUE "Select DESTINATION folder" --radiolist --width 400 --height 200)"
if [ -z "$SELECT_DESTINATION_FOLDER" ]; then CleanUp; exit 1; fi
GET_DESTINATION_FOLDER DESTINATION_FOLDER
if [ -z "$DESTINATION_FOLDER" ]; then CleanUp; exit 1; fi
else
cd "$DESTINATION_FOLDER"
{ mkdir "$ROOT_FOLDER_NAME" >/dev/null 2>/dev/null; }||{
zenity --warning --width=400 --height=200 --text "ERROR: Cannot make root dir: \"$ROOT_FOLDER_NAME\" into destination folder: \"$DESTINATION_FOLDER\"!"
CleanUp; exit 1
}
#cd "$ROOT_FOLDER_NAME"
for i in $(seq 1 $count3); do
eval current_file_or_folder_to_copy="\"\$file_or_folder_to_copy3_$i\""
if [ ! -e "$ROOT_FOLDER/$current_file_or_folder_to_copy" ]; then
error="true"
current_error="ERROR: File \"$current_file_or_folder_to_copy\" does not exist in root (source) folder \"$ROOT_FOLDER\"!"
count5=$((count5 + 1))
eval error_files_$count5=\"\$current_error\"
fi
done
{
for i in $(seq 1 $count3); do
cd "$DESTINATION_FOLDER/$ROOT_FOLDER_NAME"
eval current_file_or_folder_to_copy="\"\$file_or_folder_to_copy3_$i\""
ProcExtractFoldersFromPath current_file_or_folder_to_copy folders_array count4
for j in $(seq 1 $((count4 - 1))); do
eval current_dir="\"\$folders_array_$j\""
{ mkdir "$current_dir" >/dev/null 2>/dev/null; }&&{
cd "$current_dir"
}||{
error="true"
current_error="ERROR: Cannot make dir: \"$current_dir\"!"
count5=$((count5 + 1))
eval error_files_$count5=\"\$current_error\"
break
}
done
{
cp -r "$ROOT_FOLDER/$current_file_or_folder_to_copy" "." 2>/dev/null
}||{
error="true"
current_error="ERROR: Cannot copy: \"$current_file_or_folder_to_copy\" from \"$ROOT_FOLDER/\" to \"$DESTINATION_FOLDER/\"!"
count5=$((count5 + 1))
eval error_files_$count5=\"\$current_error\"
}
done
#Remove hidden folders and files (that start with "."):
cd "$DESTINATION_FOLDER/$ROOT_FOLDER_NAME" && {
for i in $(seq 1 $count3); do
eval current_file_or_folder_to_copy="\"\$file_or_folder_to_copy3_$i\""
if [ -d "$current_file_or_folder_to_copy" ]; then
find "." -type d -a -path "*/.*" -a -not -path "." -exec rm -r "{}" >/dev/null 2>/dev/null \;
else
find "." -not -type d -a -path "*/.*" -a -not -path "." -exec rm -r "{}" >/dev/null 2>/dev/null \;
fi
done
}
printf "Done\n"
}|zenity --progress --pulsate --auto-close --width=400 --height=200 --title="Copying files... " --text="Please wait..."
if [ "$error" = "false" ]; then
zenity --info --width=400 --height=200 --text "Files were successfully copied."
elif [ "$error" = "true" ]; then
zenity --warning --width=400 --height=200 --text "ERROR: Errors were encountered while trying to copy files!\nPlease see the next errors list:"
errors_list=""
zenity --list --width 1000 --height 500 --column "Errors" $(printf $(for i in $(seq 1 $count5); do eval printf '%s' "\"\$error_files_$i\" "; done))
fi
break
fi
done
fi
main_count=$((main_count+1))
done
#Restore previous settings:
CleanUp
并且它起作用了。因此,在您的情况下,如果您尝试从基本模板加载 {% load static %}
,您只需要像这样添加它:
gameprofile.html
答案 4 :(得分:1)
对于Django 2.2及更高版本,您必须先在html模板中加载 staticfiles ,然后再使用 static 关键字
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
还必须检查是否在 setting.py
中定义了 STATIC_URL最后确保静态文件存在于定义的文件夹中
答案 5 :(得分:0)
包含自定义标记的应用必须位于INSTALLED_APPS
中。那么您确定您的目录位于INSTALLED_APPS
吗?
包含自定义标记的应用必须位于
INSTALLED_APPS
,才能使{% load %}
代码生效。这是一个安全功能:它允许您在单个主机上托管许多模板库的Python代码,而无需为每个Django安装启用所有模板库。
答案 6 :(得分:0)
在gameprofile.html
中,请将标签{% endblock content %}
更改为{% endblock %}
,然后它将起作用,否则django将不会加载 endblock 并给出错误消息。
答案 7 :(得分:0)
{% load static %}
请将此模板标签添加到HTML或基本HTML文件的顶部
答案 8 :(得分:0)
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
在模板中,使用静态模板标记通过配置的STATICFILES_STORAGE
为给定的相对路径构建URL。
答案 9 :(得分:-1)
您需要更改:
{% endblock content %}
到
{% endblock %}