Django - 应用程序的静态文件加载到模板中,而不是另一个

时间:2017-09-02 14:22:22

标签: django django-templates

我有一个关于应用程序中的静态文件的疯狂问题(Django-Autocomplete_Light,DAL)我正在使用它来实现表单自动完成.. https://github.com/yourlabs/django-autocomplete-light/issues/907

我可以在我的主页中加载DAL表单,该表单正在扩展base.html。相同的DAL表单在另一个扩展相同base.html的模板中不起作用。我的意思是,DAL JS文件没有加载到另一个模板中..

根据DAL的doc的要求,字符{{ form.media }}在jquery导入后位于base.html

可能是什么问题?

这是未在其他模板中加载的内容:

<link href="/static/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link href="/static/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>

修改

这是我的base.html

{% load static %}
{% load i18n %}
{% load helper_tags %}

<!doctype html>
<html lang="it">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>MySoundList | {% block title %}Home Page{% endblock %}</title>
    <meta name="description" content="MySoundList">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css" media="all" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    {% block specific_css %}{% endblock %}


    {% get_current_language as LANGUAGE_CODE %}
    {% get_available_languages as languages %}

    <link rel="alternate" hreflang="x-default" href="{% change_lang %}">

    {% for lang_code, lang_name in languages %}
        <link rel="alternate" hreflang="{{ lang_code }}" href="{% change_lang lang_code %}">
    {% endfor %}

    <link rel="stylesheet" type="text/css" href="{% static 'custom.css' %}" />

</head>
<body>
<!--[if lt IE 8]>
    <p class="browserupgrade">
        You are using an <strong>outdated</strong> browser.
        Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.
    </p>
<![endif]-->

<div class="MainWrapper">
    {% block content %} {% endblock %}
</div>

<script
        src="https://code.jquery.com/jquery-3.1.1.min.js"
        integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
        crossorigin="anonymous"></script>
<script
        src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
        integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
        crossorigin="anonymous"></script>

{{ form.media }}

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"></script>

{% block specificjs %}{% endblock %}

<link rel="stylesheet" type="text/css" href="{% static 'custom.css' %}" />

<script src="{% static 'custom.js' %}"></script>

</body>
</html>

这是表单无效的页面:

{% extends 'base.html' %}

{% load static %}
{% load i18n %}
{% load helper_tags %}

{% block title %}
    {{ djname }}
{% endblock %}

{% block content %}

    <div>
        {{ event_track_form }}
    </div>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

在评论中讨论的解决方案是将正确的form对象传递给模板。如果在视图中将其定义为form_whatever,则必须在模板中使用form_whatever.media