【问题标题】:Not getting the expected layout(design) on my Django website在我的 Django 网站上没有得到预期的布局(设计)
【发布时间】:2019-10-31 00:14:00
【问题描述】:

我正在使用 django 设计一个网站,按照我最近注册的一门课程的步骤进行操作。所有代码和资源都已提供给我。我相应地遵循每一步,但我的网站布局与我所遵循的不同。 当我尝试在当前项目之前尝试过的类似项目时,我遇到了类似的问题。我的网站布局太简单了。我正在使用最新版本,所以我想我可能需要包含一些内容。该图像也未显示在我的网站上。 如果有人让我知道我错过了什么,那将非常有帮助。

This is what I'm expecting:/

This is what I'm getting:(

这是我的主要 html 文件:-

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="{% static 'css/all.css' %}">
    <!-- Bootstrap -->
    <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
    <!-- Custom -->
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <!-- Lightbox -->
    <link rel="stylesheet" href="{% static 'css/lightbox.min.css' %}">
    <title>Django Website</title>
</head>
<body>
    <!--Top bar-->
    {% include 'partials/_topbar.html' %}
    <!--Nav bar-->
    {% include 'partials/_navbar.html' %}
    {% block content %}
    {% endblock %}
    <!--Footer-->
    {% include 'partials/_footer.html' %}
    <script src="{% static 'js/jquery-3.3.1.min.js' %} "></script>
    <script src="{% static 'js/bootstrap.bundle.min.js' %} "></script>
    <script src="{% static 'js/lightbox.min.js' %} "></script>
    <script src="{% static 'js/main.js' %} "></script>
</body>
</html>

这是我的 settings.py:-

"""
Django settings for Django_Project project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/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__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'gwdbwxvan^9g5z-r$g9rf=!zeqrvu*2@^uye!ae#95my3dqp&t'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'pages.apps.PagesConfig',
    '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 = 'Django_Project.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 = 'Django_Project.wsgi.application'


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

STATIC_ROOT= os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'Django_Project/static')
]

【问题讨论】:

  • 能否分享一下html代码或者通过{% load static %}查看是否加载静态文件
  • 是的,我也用过 {% load static %}

标签: django python-3.x


【解决方案1】:

您需要在终端中收集静态文件:

django-admin collectstatic

还有here is the official documentation how to handle static files

除非在模板(html 文件)中,否则扩展布局有问题。在这种情况下,您可能会找到 this link helpful and this

如果你能提供真正有用的 HTML 文件的代码。

在你的设置文件最后添加这些行

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

【讨论】:

  • @Kalpana 我已经编辑了我的答案,请告诉我它是否有效
【解决方案2】:

我认为您在静态文件和媒体文件路径方面做错了。 请检查您的静态文件路径和媒体路径。

例如,您应该在 settings.py 文件中定义以下内容

# Static files (CSS, JavaScript, Images)
import os
BASE_DIR = os.path.dirname(__file__)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

另外,请确保项目中存在静态文件和媒体文件夹。

【讨论】:

    【解决方案3】:

    您需要在每个模板中导入您的静态文件夹

    {% 加载静态 %}

    然后您可以访问您的静态文件并将其放在图像“scr”标签中。

    src="{% static 'img/your_image.svg' %}"

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 2021-06-11
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 2018-02-10
      • 2022-01-24
      相关资源
      最近更新 更多