0

我想在1.10中启用Django模板,并且它的行为不正确。我所引用的任何从静态文件(./manage.py collectstatic)中调用的东西都不会显示HTML中引用的位置。Django 1.10模板/静态文件不显示任何图像

我有它在我的观点进口:

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") 

,并从我的基地的一些例子。 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?

回答

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' %}"> 

此图片应该是在目录结构中 '静电/ IMG /组合-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')) 
] 

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