Django Rest Framework Localization - python

I was wondering, if is there any way to add localization to django-rest-framework api. Because I want to change default message errors.

This is not supported well in Django REST framework 3.0, and it's not supported at all in versions before that.
Most of the strings that DRF uses to display things such as error notices are translatable, and some of them match the Django translations, but for the most part you need to manually find out the translation strings and do the translations on your own. The other option is to override the exception handler and translate within there.
With that said, Django REST framework 3.1 provides a guide on translating and is hooked up to Transifex. If you want your translation included by default in 3.1 (and other future releases), help us and recommend translations on Transifex.

Related

Which languages are supported by django as part I18N, Localization

I'm trying to do Internationalization using Django. I've followed through docs and able to set it up right.
My questions here are
i) What are all the language supported by django?
ii) I've created '.po', '.mo' message files for the languages 'de' , 'de-at' but when trying to access those resources from API I'm only able to get resources from 'de' for both the cases. Does Django overrides 'de-at' by 'de' message file resources. Why is it so?
I've created message files using below commands by executing in project directory.
django-admin.py makemessages -l de
After running above command. I've added resources for both the languages corresponding in their '.po' file
django-admin.py compilemessages
Any help would be appreciated.
de-at is not supported by Django. So by default Django takes de message files.
Django documentation says :
In each of these places, the language preference is expected to be in the standard language format, as a string. For example, Brazilian Portuguese is pt-br.
If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies de-at (Austrian German) but Django only has de available, Django uses de.
Hence It returns messages inside a file 'de'
If the question is actual this might help you.
I had task to translate website both to de and de-at.
Django supports de-at language.
List of supported languages:
https://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx
Or you can find on git language_info, I saw list of supported languages there too.
If you created in languages de-at - dash is separator and folder in locale named de_AT ( makemessages de_AT) it is enough. Django defines them as different languages. Problem may be in form you are using on front-end side. In my case form that I got from django documentation crashes on sublanguages so try to change your front-end form, you can find them in another stackoverflow questions.

Want to learn django with REST without rest frameworks

I am a php programmer, I have built some REST based solutions in php. Now I am learning python/django. I want to make a REST based solution in Django ( only for knowledge purpose ). I do not want to use any of REST frameworks/toolkits as This project is more a exploring django/python say how they work with raw REST concept.
I searched on net, But examples/tutorial filled on already built solutions. I also checkout for request method based filtering. I am thinking of two approaches.
Either urls.py have way to check request method and transfer to respective method in views.py.
Or I can add a pre load hook/class which determine request method on application initialize, And called respective method so overriding urls.py behavior (my preferred method).
If anybody can suggest a django way to do this?
Update : I found some interesting comments on SO, like https://stackoverflow.com/a/20898410/1230744 AND https://stackoverflow.com/a/1732520/1230744. Need to check if they can have the solution, I am searching.
Well I get the answer of my questions finally from following link. It is possible through using Class based Views + serialization.
Restful routes and Django
Snippet links in side above link gave pretty much example to gave quite picture of how one can create a REST Api using only Django Core. Also I used serialize https://docs.djangoproject.com/en/dev/topics/serialization/ for Json encoding
( Now if anybody prefer, he can flag duplicate the question. ;) )
You can start from learning the code of this projects:
http://tastypieapi.org/ Tastypie
http://www.django-rest-framework.org/ Django REST framework
They are snadrd de facto for REST API for Django and their code could be a good starting point.
Also, please review this questions:
Creating a REST API for a Django application
Adding REST to Django
Django and Restful APIs

As Django databrowse is deprecated in django 1.4, is there a better way for databrowsing?

From this question, I know that django.contrib.databrowse is unable to set custom queryset (like filter in django.admin). But I think the way of databrowse to inspect the model and to create the inline table automatically is awesome (In django.admin, I'll have to write my own TabularInline class for each model manually, which is quite suffering).
My question is: as django databrowse is deprecated in django 1.4, is there a better way(for example, another databrowse library) to do the same task?
And is it recommended to use django admin or django databrowse to make the web interface for users to browse the data?
Here's the same app that somebody else adopted it since its deprecation from django:
https://github.com/Alir3z4/django-databrowse
It's available on pypi too;)

How to customize admin filter in Django 1.4

I am a newbie in Python and Django Development, I learned a lot from the easy read examples provided by the community. But recently I want to implement a customized admin filter for the admin console shipped with Django. I searched a lot and only found some out-of-date approaches to get it done. Such as:
Custom Filter in Django Admin on Django 1.3 or below
I tried to read the source code for the filters module in 'django.contrib.admin' app, but unfortunately I can hardly understand the rationale behind the codes. So I wonder whether some kind people could supply some examples or references to this issue --- How to customize admin filter in Django 1.4 ?
Thanks in advance!
There is new django.contrib.admin.SimpleListFilter introduced in v1.4 meet your need, and official document provide sample code and easy to read.
search SimpleListFilter in this section.

Untrusted templates in Python - what is a safe library to use?

I am building a library that will be used in several Python applications. It get multilingual e-mail templates from an RMDBS, and then variable replacement will be performed on the template in Python before the e-mail is sent.
In addition to variable replacement, I need the template library to support if, elif, and for statements in the templates.
I use Mako for most my projects, and also looked at Tempita as it doesn't provide a lot of features I don't need.
The concern I have is untrusted code execution - can someone point me at a template solution for Python that either does not support code execution, or will allow me to disable it?
From the Django book:
For that reason, it’s impossible to call Python code directly within Django templates. All “programming” is fundamentally limited to the scope of what template tags can do. It is possible to write custom template tags that do arbitrary things, but the out-of-the-box Django template tags intentionally do not allow for arbitrary Python code execution.
Give Django templates a try. It's a little tricky to set up outside of a Django app -- something to do with DJANGO_SETTINGS_MODULE, search around -- but may be trusted.
Have you checked out Jinja2? It's pretty much what you're talking about, and it's a great mix of powerful while keeping things simple and not giving the designer too much power. :)
If you've used Django's template system, it's very similar (if not based off of?) Jinja.

Categories

Resources