LM19 2022-02-18 18:01 采纳率: 61.5%
浏览 514
已结题

VUE打包后,Django运行显示为白屏(无法调用index)

我使用vue编写的前端界面,单独用npm run dev是可以显示的,打包后生成dist文件夹,点击里面的index.html也是可以打开显示的。同时我使用Django搭建的后台框架,调试也是通过的。
但是Django通过url调用打包后的index就显示不出来(是一个白屏)。请大家帮看看是哪里配置出了问题。谢谢

路由文件:
SYS\urls.py

from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView


urlpatterns = [
    path('admin/', admin.site.urls),
    path('app1/', include('myDjangoVue.urls')),
    path('vue/', TemplateView.as_view(template_name="index.html")),
]

img

配置文件:
SYS\settings.py

import os
from pathlib import Path


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-svs83xni2uwxzc5c$$#0y@dfdl!tu+a3p(a+pb^4j^yoa=#tu1'

# 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',
    'myDjangoVue',
]

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',
    # # 定义跨域中间件
    # 'corsheaders.middleware.CorsMiddleware',
]

ROOT_URLCONF = 'SYS.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'myVuefront\dist')],
        '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',
            ],
        },
    },
]

# Add fro vue.js

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

WSGI_APPLICATION = 'SYS.wsgi.application'

# 设置可跨域范围
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True

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

DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': BASE_DIR / 'db.sqlite3',
    # }
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 数据库引擎
        'NAME': 'QualityDataSystem',  # 数据库名称
        'USER': 'root',  # 用户名,可以自己创建用户
        'PASSWORD': 'sunrise',  # 密码
        'HOST': '127.0.0.1',  # 数据库主机IP,如保持默认,则为127.0.0.1
        'PORT': '3306',  # 数据库端口号,如保持默认,则为3306
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.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/3.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/3.2/howto/static-files/



# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

img

static文件夹的存放位置:

img

结果显示为白屏:

img

点击F12有4个报错

img

点开一个报警显示如下:

img

  • 写回答

8条回答 默认 最新

  • LM19 2022-02-23 18:09
    关注

    终于搞定了,在vue项目文件夹下的vue_config.js里将publicPath从’/’ 修改为’../../’

    module.exports = {
      /**
       * You will need to set publicPath if you plan to deploy your site under a sub path,
       * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
       * then publicPath should be set to "/bar/".
       * In most cases please use '/' !!!
       * Detail: https://cli.vuejs.org/config/#publicpath
       */
      publicPath: '../../',
      outputDir: 'dist',
    
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月24日
  • 已采纳回答 2月23日
  • 创建了问题 2月18日

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄