Django模板变量在静态路径中

时间:2021-11-27 20:21:33

I have a template where I need to access a directory based on the user's fields.

我有一个模板,我需要根据用户的字段访问目录。

The code looks like this:

代码如下所示:

{% with user.client.company as company %}
{% static 'uploads/{{ company }}' as files %}
{% endwith %}

I also tried:

我也尝试过:

{% static 'uploads/{% user.client.company %}' as files %}

How can I achieve this? I want the folder name to be equal to user.client.company but right now I get an error like:

我怎样才能做到这一点?我希望文件夹名称等于user.client.company但是现在我收到如下错误:

Invalid block tag: 'static', expected 'endwith' for the first one and

无效的块标记:'静态',第一个预期的'endwith'和

Invalid block tag: 'static' for the second

无效的块标记:第二个是“静态”

Obviously I am doing it wrong. Any help would be much appreciated.

显然我做错了。任何帮助将非常感激。

It was pointed out I should use media rather than static. But even so, I am unsure how to dynamically set the path based on the user's parameters.

有人指出我应该使用媒体而不是静态媒体。但即便如此,我也不确定如何根据用户的参数动态设置路径。

1 个解决方案

#1


1  

remember to load you statics!!

记得加载你的静力!

{% load staticfiles %}

#1


1  

remember to load you statics!!

记得加载你的静力!

{% load staticfiles %}