Django 1.10模板/静态文件未显示任何图像 [英] Django 1.10 Templating/staticfiles not showing any images

查看:111
本文介绍了Django 1.10模板/静态文件未显示任何图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to enable Django templating in 1.10 and it is not behaving correctly. Anything I have referenced to be called from the static files (./manage.py collectstatic) is not showing where referenced in the 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

here is my 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

My settings.py

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

以及来自base.html的一些示例

and some examples from my 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' %}">

我曾尝试像django文档所建议的那样将文件放入游戏/静态/游戏/img中.但我也曾尝试与/games/static/img一起使用,但似乎都不起作用.使用网络检查工具,我在被调用的图像和JS上遇到了404错误.

I have tried putting my files in games/static/games/img like the django documentation suggests. But I have also tried to work with /games/static/img but neither seem to work. Using the web inspect tool i get a few 404 errors on the called images and JS.

1.10是否缺少某些内容?

Is there something I am missing with 1.10 ?

推荐答案

设置文件中存在问题.将该设置用于静态文件.

You have issue in your settings file. Use that settings for static files.

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

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

In your template you can load image like

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

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

This image should be available in that directory structure 'static/img/portfolio-1.jpg'.

Urls.py应该看起来像这样.

Urls.py should look like this.

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

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

please update your urls accordinlgy. This is just an example.

这篇关于Django 1.10模板/静态文件未显示任何图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆