开发者

request is missing from context

I'm trying to use django-localeurl in one of my project, but following the docs I just get an Error. What I want, is make work this code in the template:

    {% 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 %}

it's from: http://packages.python.org/django-localeurl/usage.html

I got this error

    Caught AssertionError while rendering: URL must start with SCRIPT_PREFIX: 

The problem is in this line:

    <li><a href="{{ request.path|chlocale:lang.0 }}">{{ lang.1 }}</a></li>

request.path is an empty string. but why? in the browser I can see 127.0.0.1/hu/, so If I'm right It should contain /hu/.

I created a brand new project just with django 1.3 and django-localeurl in the virtual environment, for simplicity.

My settings.py looks like (the important parts as I know):

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

LANGUAGE_CODE = 'hu'

TEMPLATE_LOAD开发者_开发百科ERS = (
    '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',
)

What Am I missing?

Edit 1: I can put the request.path manually to the context:

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

than I use rpath in the template instead of request.path, but, but.... request.path should contain something, because of django.core.context_processors.request in the TEMPLATE_CONTEXT_PROCESSORS.


The problem was not related to localeurl, with the following view works:

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

I thought putting django.core.context_processors.request into TEMPLATE_CONTEXT_PROCESSORS do the job, but not.


Locale URL middleware hacks request.path I think what you're looking for is request.path_info


In your view, when returning a response, specify the context_instance like this:

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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜