📜 ⬆️ ⬇️

Russian application names in Django

This note, the fruit of the works of Habr-man sevenov , and honor him, so that in the future he had enough karma to leave his mark on Habré himself;)

~~~~

Today there was a question about how to do this:
')
Django app names

After a half-hour googling I stumbled upon a few tickets on code.djangoproject.com (I ’m not going to link to it now, look for laziness in history). In general, their essence is that you need to rewrite almost half of Django. A little thought, I realized it in my own way. Perhaps someone has already done this, but I did not stumble upon the solution.

So let's start by writing your own tag. I called it my_app_name. The code is simple:

#my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  1. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  2. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  3. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  4. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  5. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  6. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  7. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  8. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  9. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
  10. #my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .
#my_project/apps/lib/templatetags/app_tags.py from django import template register = template.Library() def my_app_name(app_name): try : app = __import__(app_name.lower()) return app.app_label except: return app_name my_app_name = register.simple_tag(my_app_name) * This source code was highlighted with Source Code Highlighter .


It remains only to overload the admin / index.html template. Download our tag

{% load app_tags %}

find the string

< caption >< a href ="{{ app.app_url }}" class ="section" > {% blocktrans with app.name as name %}{{ name }}{% endblocktrans %} </ a ></ caption >

* This source code was highlighted with Source Code Highlighter .

and replace with

< caption >< a href ="{{ app.app_url }}" class ="section" > {% my_app_name app.name %} </ a ></ caption >

* This source code was highlighted with Source Code Highlighter .

It remains only in the directory with your Django-application in __init__.py to register

app_label= u' '

Source: https://habr.com/ru/post/46087/


All Articles