在Django中提供静态文件

时间:2014-03-28 13:06:27

标签: django twitter-bootstrap django-staticfiles

我对Django很新,我有一个问题,我已经很久没能解决了。非常感谢任何提示!

问题是为什么它找到base.html的静态文件而找不到它的login.html(它们甚至在同一个目录中)?一切正常,django设法找到模板" index.html",扩展" base.html"并以一个很好的bootstrap示例显示它。但是,当我点击登录,然后转到帐户/登录(我使用django-allauth)时,我收到错误:

"GET /accounts/login/static/twitter_bootstrap/dist/css/bootstrap.css HTTP/1.1" 404 7044
"GET /accounts/login/static/twitter_bootstrap/dist/js/bootstrap.js HTTP/1.1" 404 7038

因此,它没有加载bootstrap和js ..我尝试了很多变通方法,调试显示它指向正确的路径..登录/注销/等的过程。工作得很好。

我想要做的是在base.html中定义我想要使用bootstrap文件,然后简单地将此行为扩展到其他模板。有人可以帮帮忙吗?

我的静态文件以静态名称存在于项目文件夹中。

-Project Dir
  -static
     -static
         -twitter_bootstrap
     -static-only
     -media
     -templates
         -account
             -base.html
             -login.html
         -signups(app name)
             -index.html

settings.py

BASE_DIR = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATICFILES_DIRS = (
    os.path.join(os.path.dirname(BASE_DIR), "static", "static"),
)
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "templates"),

base.html文件:

{% load url from future %}
{% load staticfiles %}

<!DOCTYPE html>
<html>
<head>
{% block headbootstrap %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Basic css -->
<link href="static/twitter_bootstrap/dist/css/bootstrap.css" rel="stylesheet">
{% endblock %}
<title>{% block head_title %}{% endblock %}</title>
{% block extra_head %}
{% endblock %}
</head>
<body>
{% block header %}
    <div class="navbar navbar-fixed-top navbar-inverse" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <!-- Mobile Nav Button -->
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <!-- END Mobile Nav Button -->
          <a class="navbar-brand" href="#">HumanPulse</a>
        </div>
        <!-- Navigation Links -->
        <div class="collapse navbar-collapse">
          {% if request.user.is_authenticated %}
    <!—Show Home and News -->
            <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#News">News</a></li>
            </ul>
          {%else%}
    <!—Show Dashboard and Data -->
            <ul class="nav navbar-nav">
            <li class="active"><a href="#">Dashboard</a></li>
            <li><a href="#yourdata">Data</a></li>
            </ul>
          {% endif %}
        <!-- END Navigation Links -->
          <form class="navbar-form navbar-right" role="form" method="post" action="{% url 'account_login' %}">
            {% csrf_token %}
            {{ form.non_field_errors }}
            {% if request.user.is_authenticated %}
                <a class="btn btn-success" type="submit" href="/accounts/logout/" >Logout</a>
                {% if request.user.first_name or request.user.last_name %}
                    {{ request.user.first_name }} {{ request.user.last_name }}
                {% else %}
                    {{ request.user.username }}
                {% endif %}
            {% if request.user.profile.account_verified %} (verified) {% else %} (unverified) {% endif %}

            {% else %}
                <a class="btn btn-success" type="submit" href="/accounts/login/" >Login</a>
            {% endif %}
            {% if redirect_field_value %}
            <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
            {% endif %}
          </form>
        </div><!-- /.nav-collapse -->
      </div><!-- /.container -->
    </div><!-- /.navbar -->
{% endblock %}

{% block body %}
 <div class="container">

  <!-- Main component for a primary marketing message or call to action -->
  <div class="jumbotron">
    <h1>Navbar example</h1>
    <p>This example is a quick exercise to illustrate how the default, static and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p>
    <p>To see the difference between static and fixed top navbars, just scroll.</p>
    <p>
      <a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs &raquo;</a>
    </p>
  </div>

</div> <!-- /container -->
<div class="container">
{% block content %}
{% endblock %}


</div> <!-- /container -->

{% endblock %}
{% block extra_body %}
{% endblock %}
{% block footer %}
<footer>
    <p>&copy; Blog 2013</p>
    </footer>
{% endblock %}
<script src="static/twitter_bootstrap/dist/js/bootstrap.js"></script>

的index.html

{% extends 'account/base.html' %}

{% block head_title %}ProjectName{% endblock %}

{% block content %}
<p>Voila!</p>
{% endblock %}

的login.html

{% extends "account/base.html" %}
{% load i18n %}

{% block head_title %}{% trans "Sign In" %}{% endblock %}

{% block content %}
<a href="/accounts/facebook/login/"><div class="fb"></div></a>
{% include "socialaccount/snippets/login_extra.html" %}
<div class="inset">
    <div class="header">Or sign in directly</div>
    <form class="login" method="POST" action="{% url 'account_login' %}">
      {% csrf_token %}
        {{ form.non_field_errors }}
        <input id="id_login" class="login-input" maxlength="30" name="login" placeholder="Username" type="text" />{{ form.login.errors }}<br>
        <input id="id_password"  class="login-input" name="password" placeholder="Password" type="password" />{{ form.password.errors }}<br>
        <div class="remember-forgot-section">
            <input id="id_remember" name="remember" type="checkbox" />
            <label for="id_remember">Remember Me</label>
            <a class="forgot" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
        </div>
      {% if redirect_field_value %}
      <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
      {% endif %}

      <button class="btn" type="submit">{% trans "Sign In" %}</button>
    </form>

    <div class="footnote">
        Don't have an account? Login with Facebook above or <a href="{% url 'account_signup' %}">Sign Up</a>
    </div>
</div>

{%endblock%}

0 个答案:

没有答案