【问题标题】:Django static files referencingDjango 静态文件引用
【发布时间】:2021-06-29 15:42:22
【问题描述】:

我正在尝试将一些图片从静态文件夹链接到我的 Django 应用程序,但它会在模板目录中创建一个新的静态文件夹。

我的HTML

{% load static %}
<!DOCTYPE html>
{% extends 'milk_app/base.html' %}

{% load staticfiles %} 


{% block title_block %}
    Homepage
    

{% endblock %}

{% block body_block %}

<!-- Home page for Hosts -->
{% if user.userprofile.account == "Host" %}
    <div class="container">
        <div class="row">
            
            <div class="home_hover_pictures col-4">
                <a href="home.php"><img class="img-responsive" src="{% static 'images/listing-property.jpg' %}"></a>
                <h4>Create a new listing</h4>
            </div>
            
            <div class="home_hover_pictures col-4">
                <a href="home.php"><img class="img-responsive" src="{% static 'images/your-properties.jpg' %}"></a>
                <h4>Show your rented properties</h4>
            </div>

            <div class="home_hover_pictures col-4">
                <a href="home.php"><img class="img-responsive" src="{% static 'images/scroll-others.jpg' %}"></a>
                <h4>Scroll other properties</h4>
            </div>
        </div>
    </div>

<!-- Home page for Tenants (not the beer) -->
{% elif user.userprofile.account == 'Tenant' %}

<!-- Home page for not logged users -->
{% else %}
<br><br>    
    <section >
        <div>
            
        </div>
    </section>
    
{% endif %}


{% endblock %}

我的文件夹如下所示:

 1. APP_APP
 2. ACTUALAPP
 3. STATIC
        * images
              - the actual images.jpgs
 4. TEMPLATES
        * creating a new **{% static 'images** folder
              - creating a new image here

所以我的 VS 代码在我不想创建的地方创建了一个新文件,同时还创建了一个没有意义的新 .jpg 文件。为什么会这样?

【问题讨论】:

标签: html django-templates django-staticfiles


【解决方案1】:

正如 docs 中所说,如果您的资产未绑定到特定应用,您可以在设置中定义目录列表 (STATICFILES_DIRS)

STATICFILES_DIRS = [
    BASE_DIR / "static"
]

【讨论】:

  • 我把它定义为:STATICFILES_DIRS = [STATIC_DIR, ] 不是吗?而 STATIC_DIR = os.path.join(BASE_DIR,'static')
猜你喜欢
  • 2015-08-28
  • 2012-07-27
  • 2012-12-10
  • 2012-02-28
  • 2013-01-24
  • 2015-08-17
  • 2014-09-07
  • 2018-02-01
  • 2014-02-26
相关资源
最近更新 更多