[英]Displaying variable from tags file or context_processors in Django Template
According to my other question about : Handle dynamic staticfiles path with Django 根据我的另一个问题: 使用Django处理动态staticfiles路径
I am not finding the way to solve my problem. 我没有找到解决问题的方法。 I would like to insert in my HTML template a variable corresponding to a Django query. 我想在我的HTML模板中插入一个与Django查询相对应的变量。
I already used tags in order to allow some parts depending on users' groups. 我已经使用了标签,以便允许某些部分取决于用户的组。
My tags.py file looks like : 我的tags.py文件如下所示:
from django import template
from django.contrib.auth.models import Group
from Configurations.models import Theme
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return group in user.groups.all()
@register.assignment_tag
def GetTheme(Theme):
mytheme = Theme.objects.values_list('favorite_theme').last()
return mytheme
And my HTML template looks like : 我的HTML模板如下所示:
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
{% load static %}
{% load user_tags %}
<title> DatasystemsEC - Accueil </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="{% get_static_prefix %}{{ mytheme }}/css/Base_Accueil.css"/>
The goal is to pick up the variable mytheme
in my tags.py file and insert it in my html template. 目标是在我的tags.py文件中获取变量mytheme
并将其插入我的html模板中。
In return, I'm getting all the time : 作为回报,我一直在得到:
#Look double // in my url
http://localhost:8000/static//css/Base_Accueil.html
#I should get
http://localhost:8000/static/{{ mytheme }}/css/Base_Accueil.html
But, after a long moment to search a solution and with the generosity from @DanielRoseman in my previous post, I don't find the solution. 但是,经过很长一段时间搜索解决方案,并且在我之前的帖子中@DanielRoseman的慷慨解囊,我找不到解决方案。
Maybe someone had indices or ideas ? 也许有人有索引或想法?
Thank you 谢谢
I found the solution based on different answers and I will explain what I've done. 我根据不同的答案找到了解决方案,我将解释自己所做的事情。 This solution works for me with Django 1.10
: 此解决方案适用于Django 1.10
:
First step : Modify settings.py file 第一步:修改settings.py文件
I modified my settings.py file and more precisely TEMPLATES PART
. 我修改了settings.py文件,更准确地说是TEMPLATES PART
。 For the moment, this modification is just for Accueil Application but I will extend this process to all applications : 目前,此修改仅适用于Accueil Application,但我会将这一过程扩展到所有应用程序:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'debug' : DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'myapp.context_processors.context_processors_name_function'],
},
},
]
With the following example, the last line will be written like this : 在下面的示例中,最后一行将像这样编写:
# 'myapp.context_processors.context_processors_name_function'
'Accueil.context_processors.GetTheme'
Second step : Create context_processors.py file in my application 第二步:在我的应用程序中创建context_processors.py文件
I created this new file in my application part. 我在应用程序部分中创建了这个新文件。 As above, it will be extend to others applications : 如上所述,它将扩展到其他应用程序:
from django.conf import settings
from Configurations.models import Theme
def GetTheme(request):
return {'mytheme' : Theme.objects.values_list('favorite_theme').last()[0].encode("ascii")}
Third step : Modify my Base.html for Accueil application 第三步:修改Accueil应用程序的Base.html
I have a base template which manage my Accueil application. 我有一个用于管理Accueil应用程序的基本模板。 I have to write header like this is I want to take account the context_processors variable : 我必须这样写标题,因为我想考虑context_processors变量:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% get_static_prefix %}{{ mytheme }}/css/Base_Accueil.css"/>
Through this way, I can pick up the last row from my Theme
table and put the variable in {{ mytheme }}
. 通过这种方式,我可以从Theme
表中选择最后一行,并将变量放入{{ mytheme }}
。 Then, I created my good theme url. 然后,我创建了一个很好的主题URL。 Now, Django will search all css file in the good repository. 现在,Django将在正常存储库中搜索所有css文件。
From now, when I fill the formulary with a choice between two themes : Datasystems and Cameroun and validate my choice, the new theme is taken account and the global background-color change due to my theme choice ! 从现在开始,当我在配方中选择两个主题(数据系统和Cameroun)并验证我的选择时,将考虑新主题,并且由于我选择了主题,全局背景色也发生了变化!
Hopfully my answer will help others programmers ! 希望我的回答对其他程序员有帮助!
Thank you for all :) 感谢你所做的一切 :)
you can write custom context_processors
. 您可以编写自定义context_processors
。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'web', 'templates'),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
'myapp.contextprocessor.mytheme',
],
},
},
]
def mytheme(request):
return {'mytheme': 'red'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.