0

我正在尝试在 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' %}">

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

1.10 有什么我缺少的吗?

4

1 回答 1

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

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

于 2016-12-06T19:05:35.977 回答