Django无法找到图像和静态文件

时间:2017-07-17 21:21:43

标签: django server django-templates

我使用django,我的网站运作完美,但无法加载图片。我想错误的是路径,但我无法解决它。我看到这样的问题,并将所有问题都应用到我的网站上,但它们没有用,请有人帮助我,我几天都无法解决这个问题。

注意:我的网站在我的计算机上的localhost上工作正常,但不能在服务器上工作

4 个答案:

答案 0 :(得分:0)

在项目目录或app目录中创建名为static的文件夹。在名为static的{​​{1}}内创建另一个文件夹。

images文件中,添加此行。

settings.py

在模板文件中将其添加到第一行。

STATIC_URL = '/static/'

在模板中,使用这样的代码来显示图像。

{% load static %}

<img src="{% static "image/example.jpg" %}"/> 中添加以下行。

settings.py

最后转到服务器项目文件夹并使用以下命令。

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

此命令将复制静态文件夹中的所有静态文件(例如图像)并将其粘贴到python manage.py collectstatic 中提到的文件夹中,在这种情况下文件夹为STATIC_ROOT

希望这有帮助。

答案 1 :(得分:0)

我猜,这不是关于路径的。这里是另一个基本项目

enter image description here

<强>视图

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("<h1><img src='static/ben2.jpg'></h1>")

<强> settings.py

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


# 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 = 'key

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

ALLOWED_HOSTS = ['my_server_ip']


# Application definition

INSTALLED_APPS = [
    'webapp',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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 = 'web.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'web.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' ,         },         {             '名称':     'django.contrib.auth.password_validation.MinimumLengthValidator',         },         {             '名称':     'django.contrib.auth.password_validation.CommonPasswordValidator',         },         {             '名称':     '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/'

我尝试了所有的组合,就像你说的那样:D但是他们没有帮助我

答案 2 :(得分:0)

在STATIC_URL行下方,插入以下内容:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
    "use strict";

    const squaredIntegers = (arr) => {
        let arrayChoosen = arr.filter(ele => ele > 0 && Number.isInteger(ele));
        return arrayChoosen.map(x => x * x);
    }

    return squaredIntegers(arr);
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

答案 3 :(得分:0)

不确定这是否仍然令人感兴趣,但一直有同样的问题,我不得不进行一些搜索。这是解决方案 (https://docs.djangoproject.com/en/3.2/howto/static-files/deployment/ )

我的理解是 STATIC_ROOT 在您运行 collectstatic 后充当“管理存储库”,并且 Django 不会从那里提供文件。您需要设置一个单独的文件夹。这是因为在生产中,如上所述,建议有一个单独的服务器专用于此类文件。

就我而言,我只是在 static 旁边创建了一个单独的文件夹,

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'staticfiles'),)

注意,这不是推荐的地方,上面的 STATICFILES_DIRS 应该是分开的,在你的项目文件夹之外/在单独的服务器上。我把它放在那里,因为我的项目很小,用于测试目的。

正如官方文档中所指出的,每次文件更改时都需要运行 collectstatic ,然后您需要将文件复制到实际服务的目录中,即 DJANGO 实际读取的目录。

无需更改模板。

小型附加组件:对于媒体/图像文件,上述内容是必需的(至少我是这样认为的)。不适用于 css/js 文件,它在正常的“/static/”文件夹之外工作得很好。我想添加这个,因为我发现了很多关于设置这个“/static/”文件夹并收集静态的指南/解释,但几乎没有关于采取额外步骤将图像移动到一个单独的文件夹,STATICFILES_DIRS(或任何你想要的名称)设置)。

相关问题