In my webapp, I'd like to allow users who want to deploy an instance to write their own templates. Specifically, I would like to include a template for a data protection declaration using the include tag and have this point to a location which users can define in their settings.
However, this would not be translatable, as all translated strings have to be in django.po and that file is in version control.
Is there a way to extend django.po, e.g. use an include statement to point it to a second, user generated translations file, similar to how I can include templates within other templates?
Not entirely sure if this is possible, but you best bet is probably to use some other mechanism for translation. For example, you could create a template-tag user_translation and make it fetch the translation from the database or settings.
I'm a beginner to web development in django, and I'm wondering why views needed to be rendered with functions and classes? Can't the urls module just link the url to the HTML template directly rather than indirectly through a function-based view?
You can indeed have the urls module just render the HTML for you right way. For that, you can use direct_to_template: https://django.readthedocs.io/en/1.4.X/topics/generic-views.html#using-generic-views
You would rely on a function when you have any extra processing to be done before sending the response ("rendered template") to the user. You could need to log the user's IP address, for example, or you could load data from a database to fill in the template. Or you can not even have to render a HTML, but a JSON instead. That's why you would need a custom view, which would be implemented in either a function or class.
So the point of Django in general is you're going to want to be serving more than just static HTML, you'll probably want to process that in some way. As other answers have said, if you just want to return HTML, you can use a TemplateView (https://docs.djangoproject.com/en/3.0/ref/class-based-views/base/#templateview) or just have your web server server the static files (https://docs.djangoproject.com/en/3.0/howto/static-files/deployment/)
Unless your use case is a single page app, its highly likely you'll have some HTML that's common to multiple pages, in which case you can include and extend templates.
I'm trying to choose an approach I'd like to use with some part of the upcoming web-site. The part is static content, which I would usually manage with django flatpages framework, that is built in and work great. But the thing is: web site is going to be i18n in many ways and static content is one of them.
For some static content I'm going to use standard django i18n package and .po files.
Is there a way to make flatpages work in i18n way? If no, is there a way to implement that desired approach with some django-model-i18n-tool, like django-modeltranslation?
If all answers is no, what is the best practice to work around i18n static content that should be editable from some part of site, preferably admin part?
Well, there is the package django-flatpages-i18n that even includes a multilanguage menu system. It's pretty small and light-weight but will pull some dependencies like django-mptt.
Alternatively, you could use one of the Django CMS variants like django-cms or feincms. Both of them are pretty feature-complete and thus quite heavy-weight, and both will pull a number of dependencies.
And finally, you could just use a convention that all English pages start their URL with en, and then only link to those. This is the most light-weight but also the feature-poorest solution.
I have a django project utilizing flatpages for pages with primarily static content and .po-files for shorter strings, headers, buttons etc. But I'm having trouble exploring alternatives for the block of content that a customer would want to edit themselves through the admin or a customized form that I can include in templates?
I have thought about taking a CMS approach based on existing framework but because the project works great without cms-functionality for maybe 90 % of the content I do not wanna go this way, for now.
My first idea is to create a own model 'text_blocks' and use something like django-modeltranslation and populate the views context with the correct objects.
My question is first if the approach above seems reasonable but primarily what alternatives for managing this type of content am I missing.
After running my custom solution with my own app and django-modeltranslation for a while I stumbled on django-page-cms which seems to make it even simplier for customer admin users to edit pages.
Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole site should be localized, i must provide a way of translating theses names and descriptions to the users.
Is there a natural way in django to do this? Or do i have to realize it as part of the application (by representing the translations in the datamodel)
Thanks, Janosch
I would suggest checking out django-multilingual. It is a third party app that lets you define translation fields on your models.
Of course, you still have to type in the actual translations, but they are stored transparently in the database (as opposed to in static PO files), which is what I believe you are asking about.
There are two projects of note for translatable content in Django:
http://code.google.com/p/django-multilingual/
http://code.google.com/p/transdb/
I use django-multilingual for localize content and django-localeurl for choosing language based on url (for example mypage/en/).
You can see how multilingua and localeurl work on JewishKrakow.net page.
"i must provide a way of translating theses names and descriptions to the users."
"Is there a natural way in django to do this?"
Are you asking if Django can translate from language to language? Are you asking about something like http://translate.google.com/ ?
I don't think Django can translate user input into another language.
If you are going to do the translation for your users, this must be part of your data model.
Django's i18n filter allows you to have a table of translation strings. The documentation says this.
Embed translation strings in your Python code and templates.
Get translations for those strings, in whichever languages you want to support. This is something you do manually, by hiring translators or knowing a lot of languages yourself.
Activate the locale middleware in your Django settings.
I have 2 languages on my site: English and Arabic
Users can switch between languages clicking on a flag.
In models i use a proxy model:
class Product(models.Model):
name=models.CharField(max_length=100)
name_ar=models.CharField(max_length=100, default='')
def __unicode__(self):
return self.name
class Product_ar(Product):
def __unicode__(self):
return self.name_ar
class Meta:
proxy=True
In forms I use 2 forms instead of one:
class CollectionEditForm_en(forms.Form):
name = forms.CharField(label=_('Name'), max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
product = forms.ModelChoiceField(label=_('product'), queryset=Product.objects.filter(enabled=True), empty_label=None)
class CollectionEditForm_ar(forms.Form):
name = forms.CharField(label=_('Name'), max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
product = forms.ModelChoiceField(label=_('product'), queryset=Product_ar.objects.filter(enabled=True), empty_label=None)
In code check language this way:
if request.LANGUAGE_CODE=='ar':
CollectionEditForm=CollectionEditForm_ar
else:
CollectionEditForm=CollectionEditForm_en
So in templates i check:
{% if LANGUAGE_CODE == "ar" %}
{{product.name_ar}}
{% else %}
{{product.name}}
{% endif %}
Hope this solution will help somebody
It depends on who will provide the translations. If you want to provide a web interface to translation, then you need to develop that yourself, and also represent the translations in the database.
If the same translators who translated the site will also translate the data, you can provide them with the same model that they use for the site (presumably gettext), and you can then also use gettext for this content.
Also looking for content localization plugin, or how to write it.
Can add to the list django-i18n-model
I think you should operate in two steps:
Get translations
Show translated strings
For the first step, you should tell Django that the user-inserted strings are to be translated. I think there is no native way to do so. Maybe you can extract the strings from your db putting them in locale-specific files, run 'makemessages' on them, obtaint django.po files and translate.
Second, use ugettext to show those strings on your web application.
Hope this can help the ones with your same problem.
Or try this:
http://packages.python.org/django-easymode/i18n/index.html
It stays very close to how you would normally do a django model, you just add 1 decorator above your model. It has admin support for the translated fields, including inlines and generic inlines. Almost anything you can do with regular models and admin classes you can do with the internationalized versions.