【问题标题】:Django 1.10 Templating/staticfiles not showing any imagesDjango 1.10 模板/静态文件不显示任何图像
【发布时间】:2017-04-14 21:02:35
【问题描述】:

我正在尝试在 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)

我的设置.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' %}">

我已经尝试将我的文件放在 games/static/games/img 中,就像 django 文档所建议的那样。但我也尝试过使用 /games/static/img 但似乎都不起作用。使用 web 检查工具,我在调用的图像和 JS 上收到一些 404 错误。

我在 1.10 中缺少什么吗?

【问题讨论】:

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


    【解决方案1】:

    您的设置文件有问题。对静态文件使用该设置。

    # 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'))
    ]
    

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

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 2017-01-11
      • 2012-06-03
      • 2016-12-13
      • 2016-11-23
      • 2017-01-08
      相关资源
      最近更新 更多