2017-11-30 23 views
0

CSSファイルをDjangoで私のHTMLにリンクする方法を考えましたが、私の画像を私のbase.htmlファイルに表示する方法を見つけることができません。Django staticfiles画像が表示されない

これは私のファイルです。イメージを追加したのはどういうことでしょうか、私は何か間違っていますか? pic with all the files and directories まず、私は静的ディレクトリを作ってから、CSSとImagesという2つのサブディレクトリを作った。私はcssディレクトリにcssファイルを置いて、イメージディレクトリをどこに置いていますか? 次に、このコードをuptown.jpgをCSSのバックグラウンドとして追加しようとしました。

header { 
    background-image:linear-gradient(rgba(0, 0, 0, 0.5),rgba(0, 0, 0, 
0.5)), 
    url({% static 'static/images/uptown.jpg' %}); 
    height: 65vh; 
    background-size: cover; 
    background-position: center; 
    } 

しかし、画像は私のウェブサイトには表示されませんでした。私はまた、DjangoタグなしのCSSファイルで通常どおりにやりますが、まだ動作しませんでした。

私はstaticfiles_dirとallの点でsettings.pyにすべての設定をしました。だから私は問題が何であるか分からないので、助けてください。

はここにある私のURLとsettings.py:

"""ask_uptown_personal_project URL Configuration 

The `urlpatterns` list routes URLs to views. For more information please see: 
    https://docs.djangoproject.com/en/1.11/topics/http/urls/ 
Examples: 
Function views 
    1. Add an import: from my_app import views 
    2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 
Class-based views 
    1. Add an import: from other_app.views import Home 
    2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 
Including another URLconf 
    1. Import the include() function: from django.conf.urls import url, include 
    2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 
""" 
from django.conf.urls import url, include 
from django.contrib import admin 
from . import views 
from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    url(r"^$", views.HomePage.as_view(), name="home"), 
    url(r"^test/$", views.TestPage.as_view(), name="test"), 
    url(r"^thanks/$", views.ThanksPage.as_view(), name="thanks"), 
    url(r"^admin/", admin.site.urls), 
    url(r"^accounts/", include("accounts.urls", namespace="accounts")), 
    url(r"^accounts/", include("django.contrib.auth.urls")), 
    url(r"^posts/", include("posts.urls", namespace="posts")), 
    url(r"^groups/",include("groups.urls", namespace="groups")), 
] 


if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
    enter code here 

設定:

""" 
Django settings for ask_uptown_personal_project project. 

Generated by 'django-admin startproject' using Django 1.11.4. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.11/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.11/ref/settings/ 
""" 

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates') 
STATIC_DIR = os.path.join(BASE_DIR,'static') 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '[email protected][email protected]=im_($*+8mnlx-e8-yjw3qo$uy2mx' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'accounts', 
    'bootstrap3', 
    'groups', 
    'posts', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'ask_uptown_personal_project.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [TEMPLATE_DIR,], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'ask_uptown_personal_project.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.11/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.11/howto/static-files/ 


STATIC_URL = '/static/' 
STATICFILES_DIRS = [ 
    STATIC_DIR, 
] 



LOGIN_REDIRECT_URL = "test" 
LOGOUT_REDIRECT_URL = "thanks" 

最高、 感謝!

+0

あなたにお答えします。 urls.pyとsettings.pyのコードをここに入力すると助けになります –

+0

答えが編集されたので、両方のファイルがあります –

+0

私の解決策はあなたのために働いたのですか? –

答えて

1

Django documentationのように、静的ファイルへのパスをurls.pyに追加する必要があります。

from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    # ... the rest of your URLconf goes here ... 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

また、テンプレートファイルにstaticを読み込む必要があります。

{% load static %} 
<html> 
... 
関連する問題