Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的django区域设置会以这种方式失败?_Django - Fatal编程技术网

为什么我的django区域设置会以这种方式失败?

为什么我的django区域设置会以这种方式失败?,django,Django,我正在尝试创建一个同时使用法语和英语的网站,但我的Django语言环境并没有真正起作用:在我的机器上,它运行良好(使用/manage.py runserver) 但是当我在我的服务器上发送代码进行部署时 这是页面的代码: 模板 从那里我几乎可以看出,这肯定是我的服务器配置破坏的东西。但是我的服务器的设置.py和我的本地测试是相同的: import os from django.utils.translation import ugettext_lazy BASE_DIR = os.pa

我正在尝试创建一个同时使用法语和英语的网站,但我的Django语言环境并没有真正起作用:在我的机器上,它运行良好(使用
/manage.py runserver

但是当我在我的服务器上发送代码进行部署时

这是页面的代码: 模板

从那里我几乎可以看出,这肯定是我的服务器配置破坏的东西。但是我的服务器的
设置.py
和我的本地测试是相同的:

import os

from django.utils.translation import ugettext_lazy

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Just removed debug/hosts/secret key there

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',    #locale should be between sessions and common
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'BetaGames.urls'    #Just have / pointing to the "website in construction" view

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.template.context_processors.i18n',    #Needed here, think the docs said that
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'BetaGames.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),    #Nothing in the DB yet, so this is default stuff
    }
}

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

USE_TZ = True
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'static/spectre/docs/dist'),    #CSS stuff
]

LANGUAGES = (
    ('fr', ugettext_lazy('French')),
    ('en', ugettext_lazy('English')),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
我有.po文件并将它们编译成.mo,我知道traduction是有效的,因为当我用settings.py中的语言_代码手动更改lang时,它会起作用

所以我真的不知道这是从哪里来的,我很高兴有人能给我点启示


PS:我知道,由于未设置语言代码,它将默认为en us,但这正是我想要的:网站使用英语,除非有人使用法语网络浏览器。

经过一点调整后,我得到了一些有用的东西,但不是我想要的:

我在settings.py中将
MIDDLEWARE
var更改为
MIDDLEWARE\u CLASSES
,并将i18n模式添加到我的URL中:

from django.conf.urls import url
from django.contrib import admin

from django.conf.urls.i18n import i18n_patterns

import pages

urlpatterns = []

urlpatterns += i18n_patterns(
    url(r'^$', pages.views.index),
    url(r'^admin/', admin.site.urls),
)
因此,现在我可以通过
domain.com/fr/app
domain.com
重定向到
domain.com/en
访问法语翻译网站

然而,这并不是我真正想要的,因为我希望能够在不接触url的情况下完成所有这些工作


如果有人知道这样做的方法,我会接受,但我现在就开始使用。

服务器上安装的区域设置与开发机器上的区域设置相同吗?@matyas是的,locale/fr和locale/en都在repo上,并且都是在服务器上编译的
import os

from django.utils.translation import ugettext_lazy

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Just removed debug/hosts/secret key there

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',    #locale should be between sessions and common
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'BetaGames.urls'    #Just have / pointing to the "website in construction" view

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.template.context_processors.i18n',    #Needed here, think the docs said that
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'BetaGames.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),    #Nothing in the DB yet, so this is default stuff
    }
}

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

USE_TZ = True
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'static/spectre/docs/dist'),    #CSS stuff
]

LANGUAGES = (
    ('fr', ugettext_lazy('French')),
    ('en', ugettext_lazy('English')),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
from django.conf.urls import url
from django.contrib import admin

from django.conf.urls.i18n import i18n_patterns

import pages

urlpatterns = []

urlpatterns += i18n_patterns(
    url(r'^$', pages.views.index),
    url(r'^admin/', admin.site.urls),
)