Django 1.10 Templating / staticfiles没有显示任何图像

时间:2016-11-30 13:47:30

标签: django-templates python-3.5 django-staticfiles django-1.10

我试图在1.10中启用Django模板,但它的行为不正确。我从静态文件(./manage.py collectstatic)调用的任何引用都没有显示在html中引用的位置。

我在视图中导入了它:

from django.shortcuts import render, render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.forms import UserCreationForm
from django.template import loader
from django.contrib.auth.decorators import login_required
from .models import Question
from django.template.context_processors import csrf
from django.conf.urls.static import static

这是我的urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    url(r'^games/', include('games.urls')),
    url(r'^accounts/', include('allauth.urls')),
    url(r'^admin/', admin.site.urls),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我的settings.py

 STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,"static")

以及我的base.html中的一些示例

    {% load static %}
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="description" content="Hosted IT services such as Remote Backup, Disaster Recovery, Hosted Private and Hosted Shared environments, Hosted VoIP Telephony, Connectivity, IT Support and Network Monitoring, Hardware Guaranteed Fix Maintenance, Software Asset Management, Global Purchasing Strategies, Financial Services and Procurement in general.">
  <title>Hosted IT | hosting experts providing straightforward solutions</title>
  <link rel="stylesheet" type="text/css" href="{% static 'css/jquery.fullpage.min.css' %}"/>
  <link rel="stylesheet" type="text/css" href="{% static '/css/hostedit.css' %}" />
    <script type='text/javascript' src="{% static 'js/headerscripts.js' %}"></script>
    <link rel="apple-touch-icon" sizes="57x57" href="{% static 'img/ico/apple-touch-icon-57x57.png' %}">
  <link rel="apple-touch-icon" sizes="60x60" href="{% static 'img/ico/apple-touch-icon-60x60.png' %}">
  <link rel="apple-touch-icon" sizes="72x72" href="{% static 'img/ico/apple-touch-icon-72x72.png' %}">

我已经尝试将我的文件放在游戏/静态/游戏/ img中,就像django文档建议的那样。但我也尝试使用/ games / static / img,但似乎都没有用。使用Web检查工具,我在被调用的图像和JS上得到了一些404错误。

1.10有什么我想念的吗?

1 个答案:

答案 0 :(得分:0)

您的设置文件中存在问题。将该设置用于静态文件。

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles', # it's is used for static files
]


STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

STATICFILES_DIRS = (
    # os.path.join(os.path.dirname(BASE_DIR), 'static'),
    os.path.join(BASE_DIR, 'static'),
)

在模板中,您可以加载图片

{% load staticfiles %}  # register static tag
<img class="img-portfolio img-responsive" src="{% static 'img/portfolio-1.jpg' %}">

该图像应该在该目录结构'static / img / portfolio-1.jpg'中可用。

Urls.py应该是这样的。

urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^$', index, name='index'),
   url(r'^auths/', include('auths.urls')),
   url(r'^quiz/', include('quizes.urls'))
]

请更新您的网址。这只是一个例子。