使用Django处理动态staticfiles路径 [英] Handle dynamic staticfiles path with Django

查看:108
本文介绍了使用Django处理动态staticfiles路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎要克服以Django完成我的Theme Selector的问题,但我只想阻止一点:

I'm almost overcoming to finish my Theme Selector with Django but I'm blocking on one point :

==>我没有根据用户给出的格式结果来获取动态staticfiles路径.

==> I don't arrive to get a dynamic staticfiles path according to the form result given by user.

我将解释该过程:

用户通过选中RadioSelect框来填写Django表单.他有两种选择:

User fills a Django form by checking a RadioSelect box. He has a choice between two options :

  • 数据系统
  • 喀麦隆
  • Datasystems
  • Cameroun

两个选项都对应于两个具有两种不同背景颜色的主题. 数据系统为蓝色&白色,喀麦隆是绿色,红色.

Both options correspond to 2 themes which have two differents background-colors. Datasystems is blue & white and Cameroun is green & red.

因此,我选择了与位于静态文件中的两个主题之一相对应的表单结果:

So, I pick up the form result corresponding to one of both theme which are situated in static files :

|--- app1
|--- app2
├── static
│   └── Theme
│       ├── Cameroun
│       │   ├── css
│       │   │   ├── Base.css
│       │   │   ├── Base_Accueil.css
│       │   │   ├── Base_Birthcertificate.css
│       │   │   ├── Base_Configurations.css
│       │   │   ├── Base_Identity.css
│       │   │   ├── Base_Mairie.css
│       │   │   ├── Base_Recensement.css
│       │   │   └── Base_Table.css
│       │   └── images
│       │       ├── admin.png
│       │       ├── chantier.jpeg
│       │       ├── chantier.png
│       │       ├── employe?\201.png
│       │       ├── logo.png
│       │       ├── maire.png
│       │       ├── officier.png
│       │       ├── stats.jpeg
│       │       └── visiteur.png
│       └── Datasystems
│           ├── css
│           │   ├── Base.css
│           │   ├── Base_Accueil.css
│           │   ├── Base_Birthcertificate.css
│           │   ├── Base_Configurations.css
│           │   ├── Base_Identity.css
│           │   ├── Base_Mairie.css
│           │   ├── Base_Recensement.css
│           │   └── Base_Table.css
│           └── images
│               ├── admin.png
│               ├── chantier.jpeg
│               ├── chantier.png
│               ├── employe?\201.png
│               ├── logo.png
│               ├── maire.png
│               ├── officier.png
│               ├── stats.jpeg
│               └── visiteur.png

在我的settings.py文件中,我有这样的静态路径:

In my settings.py file, I have the static path like this :

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "/Etat_civil/static/Theme/"),)

我正在使用templates_tag,但不确定是否可以这样写我的函数:

I'm using templates_tag but I'm not sure if my function could be write like this :

from django import template
from Configurations.models import Theme

register = template.Library() 

def GetTheme(Theme):

    mytheme = Theme.objects.all().last()
    return  mytheme in Theme.objects.all()

在我的模板中,我想写静态路径,因为动态路径取决于用户选择的可变主题:

In my templates, I would like to write static path as a dynamic path depends on variable theme selected by user :

<!DOCTYPE html>
<html>
    <head>

    {% load staticfiles %}
    {% load user_tags %}

    <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" />
    {% if mytheme == 'Datasystems' %}
    <link rel="stylesheet" type="text/css" href="{% static 'Datasystems/css/Base_Accueil.css' %}"/>
    {% elif mytheme == 'Cameroun' %}
     <link rel="stylesheet" type="text/css" href="{% static 'Cameroun/css/Base_Accueil.css' %}"/>
    {% endif %}

    etc ....

我有点迷茫.我认为我的过程可以很好地运行,但是也许某些东西不是以pythonic方式编写的或其他方式.

I'm a bit lost. I think that my process could work pretty well but maybe something is not written as a pythonic way or something else.

我试图简洁明了.

先谢谢您

推荐答案

您可以使用

You could use the get_static_prefix template tag to do this semi-manually:

<link rel="stylesheet" type="text/css" href="{% get_static_prefix %}{{ mytheme }}/css/Base_Accueil.css"/>

这篇关于使用Django处理动态staticfiles路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆