Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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
我遇到了",;解析变量'时出现异常;将#u重定向到'&引用;运行django应用程序时_Django_Python 3.x_Nginx_Gunicorn_Ubuntu 18.04 - Fatal编程技术网

我遇到了",;解析变量'时出现异常;将#u重定向到'&引用;运行django应用程序时

我遇到了",;解析变量'时出现异常;将#u重定向到'&引用;运行django应用程序时,django,python-3.x,nginx,gunicorn,ubuntu-18.04,Django,Python 3.x,Nginx,Gunicorn,Ubuntu 18.04,我制作了一个简单的本地化应用程序(django 2.2.4)。我建立了一种改变语言的方法,并在 当我在本地使用 python manage.py runserver 它工作得很好,但当我在不同的ubuntu服务器上通过nginx和gunicorn运行它时,我得到了 Exception while resolving variable 'redirect_to' in template 'home/index.html'. Traceback (most recent call last):

我制作了一个简单的本地化应用程序(django 2.2.4)。我建立了一种改变语言的方法,并在 当我在本地使用

python manage.py runserver 
它工作得很好,但当我在不同的ubuntu服务器上通过nginx和gunicorn运行它时,我得到了

Exception while resolving variable 'redirect_to' in template 'home/index.html'.
Traceback (most recent call last):
  File "/home/administrator/environments/website/lib/python3.7/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
  File "/home/administrator/environments/website/lib/python3.7/site-packages/django/template/context.py", line 83, in __getitem__
    raise KeyError(key)
KeyError: 'redirect_to'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/administrator/environments/website/lib/python3.7/site-packages/django/template/base.py", line 835, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'RequestContext' has no attribute 'redirect_to'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/administrator/environments/website/lib/python3.7/site-packages/django/template/base.py", line 843, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'redirect_to'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/administrator/environments/website/lib/python3.7/site-packages/django/template/base.py", line 850, in _resolve_lookup
    (bit, current))  # missing attribute
django.template.base.VariableDoesNotExist: <unprintable VariableDoesNotExist object>

您可以在此处找到解决方案:

我解决问题的方法如下:

记住,在siting.py中,您必须添加

'django.middleware.locale.LocaleMiddleware',
未来的中间件

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',
   'django.middleware.locale.LocaleMiddleware',

]
此外,您还必须添加您的语言列表

_ = lambda s: s
LANGUAGES = (
    ('en', _('English')),
    ('ar', _('Arabic')),  

)
在url.py中,您的conde必须如下所示:

from django.conf.urls.i18n import i18n_patterns 
urlpatterns = [
url('admin/', admin.site.urls),
                path('', include('document_control.urls')),
            ] + static(settings.STATIC_ROOT
                        ,document_root=settings.MEDIA_ROOT)

urlpatterns += i18n_patterns( 
#url('admin/', admin.site.urls), 

url('', include('document_control.urls')), 

prefix_default_language=True, ) + static(settings.STATIC_ROOT
        ,document_root=settings.MEDIA_ROOT) #for active static files
在模板中,您的代码必须如下所示

{% load i18n %}
<ul class="nav navbar-nav navbar-right language menu">
    {% get_current_language as LANGUAGE_CODE %}
    {% get_available_languages as LANGUAGES %}
    {% get_language_info_list for LANGUAGES as languages %}
    {% for language in languages %}
        <li>
            <a href="/{{ language.code }}{{ request.get_full_path|slice:'3:' }}"
               class="{% if language.code == LANGUAGE_CODE %}selected{% endif %}"
               lang="{{ language.code }}">
                {{ language.name_local }}
            </a>
        </li>
    {% endfor %}
</ul>    




{% if   LANGUAGE_CODE  == "ar" %}
your code by arabic language
{% else %}
you code by english language


{% endif %}
{%loadi18n%}
    {%get_当前语言为语言代码%} {%get_可用语言为语言%} {%get_language_info_list for LANGUAGES as LANGUAGES%} {%用于语言中的语言%}
  • {%endfor%}
{%if LANGUAGE_CODE==“ar”%} 你的代码是用阿拉伯语写的 {%else%} 你用英语编码 {%endif%}

我希望它对您有用

您可以在这里找到您的解决方案:

我解决问题的方法如下:

记住,在siting.py中,您必须添加

'django.middleware.locale.LocaleMiddleware',
未来的中间件

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',
   'django.middleware.locale.LocaleMiddleware',

]
此外,您还必须添加您的语言列表

_ = lambda s: s
LANGUAGES = (
    ('en', _('English')),
    ('ar', _('Arabic')),  

)
在url.py中,您的conde必须如下所示:

from django.conf.urls.i18n import i18n_patterns 
urlpatterns = [
url('admin/', admin.site.urls),
                path('', include('document_control.urls')),
            ] + static(settings.STATIC_ROOT
                        ,document_root=settings.MEDIA_ROOT)

urlpatterns += i18n_patterns( 
#url('admin/', admin.site.urls), 

url('', include('document_control.urls')), 

prefix_default_language=True, ) + static(settings.STATIC_ROOT
        ,document_root=settings.MEDIA_ROOT) #for active static files
在模板中,您的代码必须如下所示

{% load i18n %}
<ul class="nav navbar-nav navbar-right language menu">
    {% get_current_language as LANGUAGE_CODE %}
    {% get_available_languages as LANGUAGES %}
    {% get_language_info_list for LANGUAGES as languages %}
    {% for language in languages %}
        <li>
            <a href="/{{ language.code }}{{ request.get_full_path|slice:'3:' }}"
               class="{% if language.code == LANGUAGE_CODE %}selected{% endif %}"
               lang="{{ language.code }}">
                {{ language.name_local }}
            </a>
        </li>
    {% endfor %}
</ul>    




{% if   LANGUAGE_CODE  == "ar" %}
your code by arabic language
{% else %}
you code by english language


{% endif %}
{%loadi18n%}
    {%get_当前语言为语言代码%} {%get_可用语言为语言%} {%get_language_info_list for LANGUAGES as LANGUAGES%} {%用于语言中的语言%}
  • {%endfor%}
{%if LANGUAGE_CODE==“ar”%} 你的代码是用阿拉伯语写的 {%else%} 你用英语编码 {%endif%}

我希望它对您有用

您是否也可以发布呈现此模板的视图。编辑以包含此视图,很抱歉在bellow中更新我的解决方案您是否可以发布呈现此模板的视图。编辑以包含此视图,很抱歉在bellow中更新我的解决方案