0

1.10에서 장고 템플릿을 활성화하려고하는데 올바르게 작동하지 않습니다. 정적 파일 (./manage.py collectstatic)에서 호출되도록 참조 된 모든 것은 html에서 참조되는 위치를 표시하지 않습니다.Django 1.10 템플릿/staticfiles에 이미지가 표시되지 않습니다.

나는 내보기에 수입이 있습니다

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에 파일을 넣으려고 시도했다. 하지만 나는 또한/games/static/img와 함께 작업하려고했지만 어느 것도 작동하지 않는 것 같습니다. 웹 검사 도구를 사용하면 호출 된 이미지와 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'), 
) 

템플릿에서는이 이미지는 그 디렉토리 구조 '정적/IMG/포트폴리오 1.JPG'에서 사용할 수 있어야

{% load staticfiles %} # register static tag 
<img class="img-portfolio img-responsive" src="{% 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')) 
] 

귀하의 URL을 업데이트하십시오. 이것은 단지 예일뿐입니다.