首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django +错误500。长邮政

Django +错误500。长邮政
EN

Stack Overflow用户
提问于 2021-03-05 19:29:00
回答 1查看 1.2K关注 0票数 0

目前,我正在尝试将Django应用程序准备好进行部署。我试图通过使用docker、dockerfile和uwsgi创建我的应用程序的映像来对我的应用程序进行“坞化”。我可以使用以下命令来完成这一任务:

代码语言:javascript
复制
sudo docker create --name container --network network --network-alias container -t -p 40001:8001 image_name 

docker build -t image_name .

图像生成,我可以启动它,我可以访问容器应用程序的主页。然而,当我试图导航到应用程序中的其他页面时,我得到了一个Server错误(500),在控制台(Firefox)中,我得到了这个错误:

代码语言:javascript
复制
 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

我的base.html里有这个

代码语言:javascript
复制
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8">

每个网页都扩展了这个页面。

我相信这可能是我的静态文件配置的一个问题,因为我正在使用WhiteNoise。但是我对我的dockerfile和uwsgi.ini文件有问题。

settings.py

代码语言:javascript
复制
import os
import socket
from pathlib import Path
from whitenoise.storage import CompressedManifestStaticFilesStorage
import docker_config

BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'LisgreyWebApp',
    'reservations',
    'takeaway',
    'food_menus',
    'crispy_forms',
    'bootstrap4',
    'corsheaders'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'LisgreyWebApp_FYP.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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 = 'LisgreyWebApp_FYP.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'lisgrey_database',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '25432'
    }
}

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',
    },
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'
CRISPY_FAIL_SILENTLY = not DEBUG

BOOTSTRAP4 = {
    'include_jquery': True,
}

LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'  # new

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = False
USE_TZ = True

STATIC_URL = "/static/"
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'dist')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

TIME_INPUT_FORMATS = [
    '%H:%M %p'
]

DATE_INPUT_FORMATS = [
    '%d-%m-%Y'
]

Dockerfile

代码语言:javascript
复制
FROM python:3.8

RUN apt-get -y update && apt-get -y upgrade && apt-get -y install libgdal-dev

# Make a working directoir in the image and set it as working dir.
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# make sure that pip & setuptools are installed and to date
RUN pip install --upgrade pip setuptools wheel

# Get the following libraries. We caan install them "globally" on the image as it will contain only our project
RUN apt-get -y install build-essential python-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info

# You should have already exported your Python library reuirements to a "requiremnts.txt" file using pip.
# Now copy this to the image and install everything in it.
COPY requirements.txt /usr/src/app
RUN pip install -r requirements.txt

# Copy everything in your Django project to the image.
COPY . /usr/src/app

# Make sure that static files are up to date and available
RUN python manage.py collectstatic --no-input

# Expose port 8001 on the image. We'll map a localhost port to this later.
EXPOSE 8001

# Run "uwsgi". uWSGI is a Web Server Gateway Interface (WSGI) server implementation that is typically used to run Python
# web applications.
CMD ["uwsgi", "--ini", "uwsgi.ini"]

uwsgi.ini

代码语言:javascript
复制
[uwsgi]
chdir = %d
# %d is the dir this configuration file is in
#socket = %dapp.sock
http = :8001
module = LisgreyWebApp_FYP.wsgi:application
chmod-socket=664

master = true
processes = 4
vacuum = true
enable-threads = true

容器日志

代码语言:javascript
复制
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.19.1 (64bit) on [Fri Mar  5 18:59:51 2021] ***
compiled with version: 8.3.0 on 05 March 2021 15:28:29
os: Linux-5.8.0-44-generic #50-Ubuntu SMP Tue Feb 9 06:29:41 UTC 2021
nodename: ebc63a00d1a7
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /usr/src/app
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /usr/src/app/
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8001 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:33037 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.8.8 (default, Feb 19 2021, 17:55:44)  [GCC 8.3.0]
Python main interpreter initialized at 0x559f4a88c290
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364600 bytes (356 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x559f4a88c290 pid: 1 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
EN

回答 1

Stack Overflow用户

发布于 2021-03-09 09:39:38

发现我试图连接到一个不同名字的数据库。检查我的数据库容器和应用程序容器的日志,以找出这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66498532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档