I have the following problem: on the main page I can change language. New language is saved in request.session['django_language']. I also have SESSION_COOKIE_DOMAIN set to my site, so session should be inherited by subdomains. And it is, because after changing language I check request.session['django_language'] in subdomains and it's fine. Then I use
django.middleware.locale.LocaleMiddleware
to translate my pages. And it works perfectly... only on main site! If I change language and refresh main site - it is ok. However, if I change language and go to a subpage (for example /LogIn), then the page is NOT translated at all. It stays on default language. This is really strange, because if I use
{% load i18n %}
{% get_current_language as lang %}
in this subpage, then lang is good language. There is no mistake. What kind of problem can it be? Some suggestions?
It seems that using ugettext was the problem. I changed to ugettext_lazy and now it works perfectly fine. For some reason ugettext translates with old language. It works fine now.
Related
I have a Django Wagtail application, which employs the templating machine Jinja (or Jinja2, not entirely sure, but won't make a difference for this issue).
When I e.g. want to use the templating to show the title of a blog post, I can write
{{ post.title }}
This works perfectly fine. But I cannot enforce hyphenation in this title. If the title holds a very long word, this crashes my layout on small devices. Anyone an idea how I can tell Jinja that hyphenation should be enabled?
I am developing a website using Django Mezzanine.
I have some pages using a model derived from Mezzanine Page model with translatable fields (using i18n) and this works.
However I have some pages relative to some events in given countries, so they are only in one language, and they use a model derived from Mezzanine Displayable model, without translatable fields but a language attribute.
In the list I tried to put links including the language of the page (like /en/events/event/eventslug ).
I tried to add translation.activate(lang_code) to the view.
This way I get the page in the wanted language (I use {% trans %} template tags).
The problem comes when I visit the page from another language (for example if I am on the filter page in Spanish and go to an English event). Then the language switch (mezzanine form sending a POST request to /i18n/) doesn't work any more (I am locked in English)
The switch still work in private navigation for example as long as I don't visit one of this pages from another language.
How can I fix this?
I am out of ideas as of why...
Update: I changed the urlpattern so it is not i18n anymore (I think it makes more sense) but as I still need translation.activate(lang_code) for the {% trans %} tags my problem is still the same.
The problem came from the fact that if you use translation.activate(language) then you need to use translation.deactivate().
So that would have required the use of a middleware to make it work properly.
However I found the i18next module allowing to override the locale in the template, so I defined the context 'lang_code' in my view and used {% overridelocale lang_code %} in my template.
The django-admin app is supposed to open a popup window when I try to create a related object. For example, clicking the green plus button here:
Should bring a new pop-up window, where I can create a new related (in this case Session) object. However, when I click the green plus button, the object create opens up in the same window, instead of a pop-up window. This is a problem, because if a user starts to create a primary object, then half-way through, clicks the green plus, he will lose all the progress on the primary object. This means that the green plus is unusable.
I have no idea why this is happening. I understand that I am not providing a minimum example of the problem, because I haven't been able to construct one. Right now I want to diagnose the problem. Any ideas of how I can diagnose this? And fix it?
I can provide any information you think is necessary.
Related: Django admin popup links are broken. But he's using Grappelli, which I am not. I am using the default django admin interface.
I am using Django version 1.8.4.
UPDATE: The popups work fine with manage.py runserver. The problem only appears with the deployed version under apache. I hope this narrows down the issue.
On google groups: https://groups.google.com/forum/#!topic/django-users/awYelJjFjHk.
I wasn't able to produce a minimal example of what was going on, but it is fixed now. Essentially, I did the following:
Upgraded to Django 1.8.6 .
Ran python manage.py collectstatic (as suggested in https://community.webfaction.com/questions/18544/there-is-not-popup-window-for-one-to-many-relationship-with-djangos-admin-v18)
So in the end I'm not sure what the issue was. I hope this is useful to someone else.
If someone else comes up with a more complete answer, which involves a minimal example and with an explanation of what was going on, I'll be glad to change the accepter answer.
This appears to be a reproducible error in 1.9.3. Testing it myself I setup a new project using the latest Django (1.9.3) and ran into the same error. Below is the fix. It seems to also work with 1.9.1 I will report to Django team now.
+++ b/contrib/admin/templates/admin/related_widget_wrapper.html
## -10,7 +10,7 ##
</a>
{% endif %}
{% if can_add_related %}
- <a class="related-widget-wrapper-link add-related" id="add_id_{{ name }}"
+ <a class="related-widget-wrapper-link add-another" id="add_id_{{ name }}"
href="{{ add_related_url }}?{{ url_params }}"
title="{% blocktrans %}Add another {{ model }}{% endblocktrans %}">
<img src="{% static 'admin/img/icon-addlink.svg' %}" alt="{% trans 'Add' %}"/>
When I change language on my Django-powered site, everything works fine except the translation of the field descriptions in the forms. The description is still displayed in the old language. Only the forms already visited that session are affected.
It seems like some sort of caching problem, but I don't use any caching (as far as I know) and all other parts of the site behave as expected (templates and random text using django.utils.translaction.ugettext work just fine).
Summarized:
When I open the site and change language, and then visit a form: everything works as expected. All text is translated.
When I visit a form, change language and return to the form: everything is translated, but the form stays in the old language.
Restarting the web server forces the language to change: then Django behaves as in (1) for the current session. Until the language is changed again.
Help is appreciated!
Kind regards,
Patrick
Fixed it by changing
from django.utils.translation import ugettext as _
to
from django.utils.translation import ugettext_lazy as _
for my forms.
I have a Django-powered page running on Apache with mod_wsgi. It works just fine in Firefox. When I switch to Internet Explorer, however, none of my links work. They all drop the domain part of the link.
For example, in Firefox, if I mouse over one of my links, I see something like this:
http://mydomain.edu/pathtomystuff/linkpage/
and it works.
However, in Internet Explorer, the same link shows this when I mouse over it:
http:///pathtomystuff/linkpage/
and obviously doesn't work.
If I manually type the address in Internet Explorer, it works fine. It's just the links.
This is probably something obvious and boneheaded. Please forgive me :)
UPDATE
Well I did figure out something of a "solution". I had BASE href= {{request.path}} in my base html file (which all other pages in my site extend). In reviewing the source code shown by IE and Firefox, both were seeing BASE href= which means request.path was not being passed to my template. So I changed it to BASE href=mydomain.edu and it works in IE now.
This is not a great fix though because it takes away from the portability of the django app...
If you want to preserve portability (as well as maintainability), the easiest and most "djangonistic" way to proceed would be to use {% url %} tags in your templates.
Note that the {% url %} tag should be passed a view as 'argument', for example: {% url myApp.views.myView foo='bar' %} will use your urlconf to create the appropriate url that will point to the myApp view called myView, with the value 'bar' passed as the 'foo' arg.
Of course, you needn't have arguments in all your views, the foo='bar' part is purely optional (Please do note that you shouldn't specify the request argument)
The most common use would be to use a syntax such as:
MyLink
Basically what you're trying to do here is reinvent the url tag, it already exists, so use it! :)