Error lookup for key [LANGUAGES] usin i118n - django

I'm having a hard time to make i118 work. When calling my html I get:
Failed lookup for key [LANGUAGES] in u"[{'projects': [<Project: etwas>]}]"
This was my work flow:
#setting.py
LANGUAGE_CODE = 'de'
ugettext = lambda s: s
LANGUAGES = (
('de', ugettext('German')),
('en', ugettext('English')),
('pt', ugettext('Portuges')),
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
#...
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.i18n',
)
#base.html
{% load i18n %}
<html>
<body>
<form action="/i18n/setlang/" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="next/page" />
<select name="language">
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option>
{% endfor %}
#projects.html
{% extends 'base.html' %}
{% load i18n %}
{% block content %}
{% for p in projects %}
<h1>{{ p.title_en }}</h1>
<p>{{ p.body_de }} </p>
<p>{{ p.body_pt }} </p>
<hr>
{% endfor %}
{% endblock %}
What am I missing? I can't find any useful information on the error message online.

It looks like you are not using RequestContext.

It seems like 'django.core.context_processors.i18n' is not called correctly.
This maybe because your view uses django.shorcuts.render_to_response without context_instance=template.RequestContext(request).
A solution is to switch to django.shortcuts.render.

Related

Django allauth - Template include in base.html causes RecursionError

I am using allauth to handle all my authentication. Using that app each piece of authentication functionality is given its own template. However, I want to include the registration form in all other templates. i.e. the registration form is present in /account/login/, /account/password/change ... etc...
I decided to achieve that by including signup.html in base.html which is extended in all other templates. Like this:
base.html
...
<h4 class="card-title text-center">Register</h4>
{% include "account/signup.html" %}
...
account/base.html
{% extends "base.html" %}
account/signup.html
{% extends "account/base.html" %}
{% load i18n %}
<p>{% blocktrans %}Already have an account? Then please sign in.{% endblocktrans %}</p>
{% for message in messages %}
<span style="color:red;">{{ message }}</span>
{% endfor %}
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{% for field in form %}
<div id="input-group">
{{ field }}
</div>
{% endfor %}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div class="card-footer text-center">
<input type="submit" class="btn" value='{% trans "Get started" %}'>
</div>
</form>
account/login.html
{% extends "account/base.html" %}
{% load static %}
{% load i18n %}
{% load account socialaccount %}
{% get_providers as socialaccount_providers %}
{% block general_notice_modal %}
{% endblock general_notice_modal %}
{% block login-modal %}
...
This code causes the following error, which only occurs when I add the {% include %} tag in base.html
I think the code below:
{% include "account/signup.html" %}
should be in "account/base.html" not in "base.html".
You have an error because your "base.html" call "account/signup.html" and your "account/signup.html" call "account/base.html" who call himself "base.html". You have a loop.

Django Haystack search inside index template isn't return anything

So I've followed the haystack documentation but I can't make the jump into my app.
Following the instructions I have got templates/search/search.html at search/ returning results but I want my index page (my frontpage) to return the search results.
Search from the index just doesn't return anything. I'm new to Django and need some help, I'm sure I need to create a view to do this but I don't know how.
My index views.py:
def index(request):
return render_to_response('index.html', {'categories': Category.objects.all(), 'entries': Entry.objects.all()[:5]})
My index.html template:
{% extends 'base.html' %}
{% load markdown_deux_tags %}
{% block title %}The NRSLTD Knowledge Base{% endblock %}
{% block content %}
<div class="search">
<h2>Search</h2>
<form type="get" action=".">
<input type="text" name="q" value="Search here" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
<button class="btn btn-custom" type="submit">Search</button>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
{{ result.object.title }}
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}{% endif %}« Previous{% if page.has_previous %}{% endif %}
|
{% if page.has_next %}{% endif %}Next »{% if page.has_next %}{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% for note in notes %}
<h1>{{ note.title }}</h1>
<p>
{{ note.body }}
</p>
{% endfor %}
</div>
<div class="wrap">
<h2 id="title">View the last five entries made and the most recent categories added.</h2>
<div class="cat">
<h2>Categories</h2>
{% if categories %}
<ul>
{% for category in categories %}
<li>{{ category.title }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no entries.</p>
{% endif %}
</div>
<div class="ent">
<h2>Entries</h2>
{% if entries %}
<ul>
{% for entry in entries %}
<li>{{ entry.title }}</li>
{{ entry.text|markdown }}
{{ entry.tags }}
{% endfor %}
</ul>
{% else %}
<p>There are no entries.</p>
{% endif %}
</div>
</div>
{% endblock %}
Urls.py:
# Search
(r'^search/', include('haystack.urls')),
I have copied the search/search.html into the index.html and it looks the same, I just need it to return the results in the same was as search/search.html does.
UPDATE:
Added settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'knowledgebase',
'markdown_deux',
'haystack',
'bootstrap3',
)
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
# This line enables signal processor that will update_index for every change in models
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

How do I use request.META.get('HTTP_REFERER') within template?

I'd like to use request.META.get('HTTP_REFERER') within template.
My template source:
<!-- this is login.html -->
{% extends "base.html" %}
{% block title %}django bookmark- login{% endblock %}
{% block head %}login{% endblock %}
{% block content %}
{% if form.errors %}
<p>try again!</p>
{% endif %}
<form method="post" action=".">{% csrf_token %}
<p><label for="id_username">username:</label>
{{ form.username }}</p>
<p><label for="id_password">password:</label>
{{ form.password }}</p>
<input type="hidden" name="next" value="/<!-- I WANT TO USE 'HTTP_REFERER' HERE -->" />
<input type="submit" value="login" />
</form>
{% endblock %}
How what should I do?
urlpatterns = patterns('', (r'^login/$', 'django.contrib.auth.views.login'),
There's no need for get.request.META is a dictionary, and as with all dictionaries, you can perform field lookup in the template using the dot notation: {{ request.META.HTTP_REFERER }}
Add django.core.context_processors.request in your settings file in TEMPLATE_CONTEXT_PROCESSORS then you would be able to use the request in template without explicitly passing it in request context.
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.request', # this one
)
the in template you could do {{request.META.HTTP_REFERER}}
Actually the preferred way is to use the next parameter as documented here
You can do in your template something like this:
<input type="hidden" name="next" value="{{ request.GET.next }}" />

Django template - inline POST as hyperlink logic

I have a simple yet frustrating problem:
in my template I have:
{% for lang in LANGUAGES %}
{% if lang.0 != LANGUAGE_CODE %}
<input type="hidden" name="language" value="{{ lang.0 }}">
{{ lang.1 }} |
{% else %}
{{ lang.1 }}
{% endif %}
{% endfor %}
The language switching works fine, it's just that the pipe separator which I want to separate the two languages which can be selected doesn't stay in the middle. Obviously when the first statement is evaluated to false in the first instance then the linked option is written last and the pipe appears at the end. Does anyone have a simple way to get a pipe separator fixed in between the two on both conditions?
maybe try using the forloop counter...? ...not to write the separator in the last item
{% for lang in LANGUAGES %}
{% if lang.0 != LANGUAGE_CODE %}
<input type="hidden" name="language" value="{{ lang.0 }}">
{{ lang.1 }}
{% else %}
{{ lang.1 }}
{% endif %}
{% if forloop.last != true %}
|
{% endif %}
{% endfor %}

django-translation: How to translate languages

The officual django doc suggest to write the following in the settings.py
ugettext = lambda s: s
LANGUAGES = (
('de', ugettext('German')),
('en', ugettext('English')),
)
With this arrangement, django-admin.py makemessages will still find and mark these strings for translation, but the translation won't happen at runtime -- so you'll have to remember to wrap the languages in the real ugettext() in any code that uses LANGUAGES at runtime.
But, I fail to understand where to wrap the code with real translation tags?
e.g. my code in template is
<form id="locale_switcher" method="POST" action="{% url localeurl_change_locale %}">
<label><b>{% trans "Language" %}:</b></label>
<select name="locale" onchange="$('#locale_switcher').submit()">
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}" {% ifequal lang.0 LANGUAGE_CODE %}selected="selected"{% endifequal %}>
{{ lang.1 }}</option>
{% endfor %}
</select>
<noscript>
<input type="submit" value="Set" />
</noscript>
</form>
The solution suggested here: Using settings.LANGUAGES with properly translated names using gettext()
Shows empty select box with no text at all on any laguage
The following code works for me:
// settings.py
ugettext = lambda s:s
LANGUAGES = (
('de', ugettext('German')),
('en', ugettext('English')),
)
// template
{% load i18n %}
{% get_available_languages as LANGUAGES %}
{% for LANGUAGE in LANGUAGES %}
<p>{{ LANGUAGE.0 }} - {{ LANGUAGE.1 }}</p>
{% endfor %}