如何解决错误的静态文件找不到Django

时间:2020-08-02 05:18:47

标签: python-3.x django static settings

我正在一个项目中,我使用了在线网站上的前端模板。 但是我想我已经将每个文件放在正确的位置,但是我却得到了静态文件未找到错误。 以下是重要文件: setting.py

root= tk()

master = Frame(root,width=300,height=300,relief=SUNKEN)
   
master.place(relx=0.1,rely=0.1,relwidth=0.8,relheight=0.8)

root.mainloop()

urls.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'lj6ra=$c5t!2kkin(qvuk3o(wui!m(%%wktf%my!c_gbl6(7ap'

DEBUG = True
ALLOWED_HOSTS = ['*']



STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'Gym/static/'),
)


# Application definition

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

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        '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 = 'Gym.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/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/3.0/howto/static-files/

STATIC_URL = '/static/'

index.html:

from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf.urls.static import  static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index),
]

我遇到错误:

    ...
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/font-awesome.min.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/elegant-icons.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/nice-select.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/owl.carousel.min.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/magnific-popup.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/slicknav.min.css' %}" type="text/css">
    <link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css">
    ...

我也尝试了STATICFILES_URL,但无法正常工作,请帮我发疯 我也尝试过收集静电,但没有任何反应,我不明白这个问题,请帮忙

1 个答案:

答案 0 :(得分:1)

您可以尝试将css文件放在此处Gym / static / Gym /#place css文件下

以及在index.html中使用以下

<link rel="stylesheet" href="{% static 'Gym/bootstrap.min.css' %}" type="text/css">