Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request is missing from context

Tags:

django

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_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',
)

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.

like image 602
balazs Avatar asked May 02 '26 20:05

balazs


1 Answers

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.

like image 98
balazs Avatar answered May 04 '26 15:05

balazs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!