2011-07-20 22 views
3

django-localeurlを私のプロジェクトに使用しようとしていますが、ドキュメントに続いてエラーが表示されます。私が欲しいのは、テンプレートで作業このコード作るです:要求がコンテキストにありません

{% load i18n %} 
    {% load localeurl_tags %} 
    {% get_available_languages as LANGUAGES %} 
    {% get_current_language as LANGUAGE_CODE %} 

    {% for lang in LANGUAGES %} 
     {% ifequal lang.0 LANGUAGE_CODE %} 
     <li class="selected">{{ lang.1 }}</li> 
     {% else %} 
     <li><a href="{{ request.path|chlocale:lang.0 }}">{{ lang.1 }}</a></li> 
     {% endifequal %} 
    {% endfor %} 

それからだ:

<li><a href="{{ request.path|chlocale:lang.0 }}">{{ lang.1 }}</a></li> 
:私は、問題は、このラインであるこのエラーに

Caught AssertionError while rendering: URL must start with SCRIPT_PREFIX: 

を得たhttp://packages.python.org/django-localeurl/usage.html

request.pathは空の文字列です。しかし、なぜ?ブラウザで私は127.0.0.1/hu/を見ることができるので、もし私が正しいならそれは/hu/を含むべきです。

私は、単純化のため、仮想環境にdjango 1.3とdjango-localeurlという新しいプロジェクトを作成しました。 (私が知っているように、重要な部品)

私のsettings.pyは、次のようになります。

LANGUAGES = (
    ('hu', 'Hungarian'), 
    ('en', 'English'), 
    ('sk', 'Slovakian'), 
) 

LANGUAGE_CODE = 'hu' 

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

MIDDLEWARE_CLASSES = (
    'localeurl.middleware.LocaleURLMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
) 

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth", 
    "django.core.context_processors.debug", 
    "django.core.context_processors.request", 
    "django.core.context_processors.i18n", 
    "django.core.context_processors.media", 
    "django.core.context_processors.static", 
    "django.contrib.messages.context_processors.messages", 
) 
INSTALLED_APPS = (
    'localeurl', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # Uncomment the next line to enable the admin: 
    # 'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
) 

私は何をしないのですか?

編集1: 私はコンテキストにrequest.path manuallyを置くことができます:私が代わりにrequest.pathのテンプレートでrpathを使用するよりも、

def main(request): 
    return render_to_response('base.html', {'rpath': request.path}) 

、しかし、しかし.... request.pathが原因django.core.context_processors.requestの、何かを含める必要がありますTEMPLATE_CONTEXT_PROCESSORSのURLミドルウェアハックは、私はあなたが探しているものだと思いrequest.path

答えて

3

request.path_infoある問題は、次のビュー作品で、localeurlに関連していなかった。

return render_to_response('base.html', {}, context_instance = RequestContext(request)) 

私は思いましたdjango.core.context_processors.requestTEMPLATE_CONTEXT_PROCESSORSに入れることはできません。あなたのビューで

0

ロケールは

+0

request.path_infoは良くありません、私が試した、と同じ結果を持っています。 – balazs

0

、応答を返すとき、このようcontext_instanceを指定は:

from django.shortcuts import render_to_response 
from django.template.context import RequestContext 

return render_to_response('base.html',{'object':object},context_instance=RequestContext(request)) 
関連する問題