Django custom locale directory - django

I'm developing a project with two different sites, divided by
language. Maybe I was terribly wrong, but now my directory structure
looks like:
/ruapp/settings.py # SITE_ID = 1
/ruapp/manage.py
/enapp/settings.py # SITE_ID = 2
/enapp/manage.py
/common/urls.py
/common/ # almost every other file
/common/templates/ # templates with {% trans %}
/locale/ # with locales ru-ru and en-us, generated by calling
makemessages from the root of all this structure
How to tell django about the locale? It does not seem like it will
find the /locale/ folder by itself

AFAIK, python-gettext can't use different folder, so... Use Symlinks, Luke!

Make sure your directory structure for localization files look like that:
locale/en/LC_MESSAGES/django.mo
locale/ru/LC_MESSAGES/django.mo
If you name them something else, or put them elsewhere, it probably won't work.
You also need to pust aproperiate l18n information in your settings file. LANGUAGE_CODE = 'en' in one, and LANGUAGE_CODE = 'ru' in the other.
Edit: Have you created two separate apps or two separate project? Apps usually don't have their own settings.py and manage.py...
You can have one project that have multiple settings files and runs multiple websites. It would be much more django-ish (and much easier to deal with) for your directory structure to look like this:
/settings_ru.py # SITE_ID = 1
/settings_en.py # SITE_ID = 2
/manage.py # use --settings option to distinguish between settings files.
/urls.py
/templates/ # templates with {% trans %}
/locale/ # locale/en/LC_MESSAGES/django.mo and locale/ru/LC_MESSAGES/django.mo
(+ other common code, inside their respective apps)

Related

Changing URL pattern in Django so it doesn't show app name

I've got a folder/app named "projects" and there I want to make a new site there, named, let's say, "cheese".
In my urlpatterns in urls.py in projects folder I've got
path('cheese/', views.cheese, name='cheese')
but the whole URL looks like domain.co/projects/cheese
I'd like to omit that projects in URL, so it could just be domain.co/cheese
Those URL-things are kinda hard to understand for me and as I couldn't properly name my problem, I couldn't find a solution out there, though I believe there must be some.
The idea of having an app inside another app is kinda weird, but anyways, that won't enforce the inner app's urls to be like <outer_app>/<inner_app>/... if you set the url patterns correctly.
Basically, you've got domain.co/projects/cheese because, you include your project's app urls as:
# main urls.py file for your project
path('projects/', include('projects.urls'))
and in your project's urls file you have:
# projects/urls.py
path('cheese/', include('projects.cheese.urls'))
So if you want to have the cheese urls as domain.co/cheese, simply add the include to the main url file:
# main urls.py file for your project
path('projects/', include('projects.urls'))
path('cheese/', include('projects.cheese.urls'))

Django Templates: How does it decide where to load a template?

I was following the Django Girls Tutorial up to the point where we add the login capabilities and I've gotten to a point where it tries to load from a different template folder than I want.
I have all my blog templates in blog\templates\blog\, etc. and that is where Django is looking for my login.html. However, in the tutorial (and as a result in my folder structures), the path for login.html is: mysite/templates/registration/login.html. So how would I make Django look there?
Sorry if this is a stupid question.
Django will look to your settings.py for how to load templates, and the order in which it should try to load templates. It's likely that you haven't configured django to look for templates in mysite/templates.
There's a setting called TEMPLATE_DIRS which is a list of absolute paths for your template folders. So in your settings.py file, try something like below. Read my comments to see what each line does.
# create a variable that stores the absolute path to your project's root directory
# you might have something like this already defined at the top of your settings.py
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
# this is the default value for TEMPLATE_LOADERS
# which says to look at the `TEMPLATE_DIRS` variable, and then in each of your app's subdirectories for templates
TEMPLATE_LOADERS = ('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader')
# the special sauce. tell django to look at the "templates" folder
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )

Use custom directory for "templatetags"

some one know if its likely use custom directory for "templatetags" eje: "my-project/templatetags"
Normal
My-Project-Name
My-App
__init__.py
templatetags
__init__.py
Need Some Like This
My-Project-Name
templatetags
__init__.py
This is possible. Just add location of your templatetags.py or templatetags directory to Django settings.py into OPTIONS in TEMPLATES setting.
In my case I put my templatetags in the libs/ directory that is located in project root dir.
You have two options, builtins or libraries:
TEMPLATES = [{
...
'OPTIONS': {
...
'builtins': [
'libs.templatetags'
],
# or as #x-yuri pointed out, you can put them in `libraries`
'libraries': {
'my_tags': 'libs.templatetags',
},
}
}]
If you use builtins, it is available everywhere and you don't need to use {% load %} for that.
If you use libraries you need to use {% load my_tags %}.
This is not possible. The reason being, templatetags must reside inside a django app.
From the documentation of templatetags:
Custom template tags and filters must live inside a Django app. If
they relate to an existing app it makes sense to bundle them there;
otherwise, you should create a new app to hold them.
The app should contain a templatetags directory, at the same level as
models.py, views.py, etc. If this doesn’t already exist, create it -
don’t forget the __init__.py file to ensure the directory is treated
as a Python package. After adding this module, you will need to
restart your server before you can use the tags or filters in
templates.

django 1.6 with django_tables2 cannot change DATETIME_FORMAT

python 2.7.5+. django_tables2 0.14.0
In settings.py:
USE_L10N = True
DATETIME_FORMAT = 'M j, Y H:i:s.u'
Didn't work. I tried {% load L10N %} at the top of my template. Didn't work.
I created a formats directory at the top of my project containing an en directory containing formats.py and added the following to my settings.py (yes, there are init.py files in the formats directory and in the en directory):
FORMAT_MODULE_PATH = '<project>.formats'
Didn't work.
I tried putting in {{created_at|date: 'M j, Y'}}{{created_at|time: 'H:i:s.u'}} before the {% render_table table %} tag from django_tables2. Got an error.
Tried the recommended django_tables2 method of, in my tables.py definition, adding to the Meta class:
localize('created_at')
Didn't work.
Tried putting 'localize=True' in the column definition for created_at in tables.py.
Didn't work.
The only thing that worked was modifying python2.7/site-packages/django/conf/locale/en/formats.py (in my virtual environment) but I won't be allowed to do that on my project because it's not considered portable.
I see stackoverflow entries that say this works but they are many years old and many django versions back. I see others here that say this doesn't work and they haven't/aren't going to fix it.
This functionality logs requests; during testing the other devs could really use seeing seconds and probably microseconds to help them debug but I can't seem to give it to them.
Any suggestions that I haven't already tried?
Thanks.
OK, blush - I got it. I made the following change in the model for the DateTimeField created_at:
#property
def nice_date(self):
# mm/dd/yyyy HH:mm:ss.uuuuuu
t = self.created_at.strftime("%m/%d/%Y %H:%M:%S.%f")
return t
Then, I displayed nice_date instead of created_at.
I hope this helps someone else.

Django template dir switching

Does anyone know a way to switch the TEMPLATE_DIR in django dynamically.
I need to create a set of templates for a mobile version and would like the templates to sit inside there own dir instead of inside the root template dir ie: I would like 2 template dirs 'templates' and 'mobile_templates' and not have to use 'templates/mobile' for the latter.
Do I have to write my own template loader?
You can set multiple template directories in your settings file and Django will search them in the order that you list them. Problem is that it doesn't care if you want template_x.html from directory a or b. If you have the same template_x in directory a and in b, it'll pull from which ever is listed first which can be confusing. A good way would be as follows:
Have only 1 template directory somewhere called 'templates'. Inside of this folder have a folder called 'mobile' and a template called 'default' (or whatever). Then when you call your template you just have to use the directory path as well.
In your view:
# some mobile view (everything omitted brevity)
get_template('mobile/some_template.html')
# some normal view (everything omitted brevity)
get_template('default/some_template.html')
In your templates:
Mobile Template:
{% extends 'mobile/base.html' %}
Normal Template:
{% extends 'default/base.html' %}
Settings File:
TEMPLATE_DIRS = (
'D:/some/path/to/templates',
)