Django makemessages not working for JS files - django

My setup
settings.py
INSTALLED_APPS = (
...
'myprojectname',
...
)
STATIC_ROOT = '/var/www/a_valid_path/'
LOCALE_PATHS = (
os.path.join(BASE_DIR, "locale"),
)
urls.py
js_info_dict = {
'domain': 'djangojs',
'packages': ('myprojectname',),
}
urlpatterns = patterns('',
...
url(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
...
)
My project structure is as follows:
|- projectname
|--- app1
|--- app2
|--- manage.py
|- virtualenv
|- static
|--- js
|--- css
I also have the locale folder in the root folder of my project, where manage.py is located.
What I'm doing
Simply running:
./manage.py -l ro -d djangojs
My problem
It's not working. No .po file is being generated. Server-side translation works, however (views + templates). I've followed all advice, and still nothing. Even tried to create the djangojs.po file myself to see if Django deletes it, or does something with it -- nope.
No error is generated, just processing locale ro is shown (for a really short time -- too short if you ask me), and that's that. Any help?
Edit: Forgot to mention that my folder containing the JS files is not inside each Django app, but in a separate location. Still, shouldn't Django look inside the STATICFILES_DIRS?

Django's makemessages only will make messages from files that are in on one of your TEMPLATE_DIRS. So any files you want to translate need to be in one of those directories.
You can do that in one several ways:
Place the *.js files in one of your TEMPLATE_DIRS as is
In-lining your JS in the html files
Place all the strings that need translation in data-attributes on the dom and grab them from the dom via JS

Are you running makemessages from a directory parent of the ones containing your JavaScript files?
Do your JavaScript file names ends with a .js?
Do you either use django.gettext('string') or _('string') to mark strings requiring translations?

I've experienced the same issue. I've discovered that the issue is reported in the Django ticket #23717: https://code.djangoproject.com/ticket/23717
Fixes are in upcoming stable 1.7.2 version: https://docs.djangoproject.com/en/1.7/releases/1.7.2/
I've installed 1.7.2 and confirmed that the issue is fixed.

I had the same problem when using the Django i18n, after many times trying, I finally got the correct answer: we need to put the .js files into the project directory, which was specified when we assign 'js_info_dict'.
But usually we put the JavaScript files in the same level catalog as project, so there is the problem. (we don't need to put the JavaScript files into templates directory).

Related

Why did I get Template does not exist error, though it appears Django offers flexibility with respect to folder structure?

I have read several posts here where people discussed their preferred directory structure to keep various files of their Django project. But I get a file-does-not-exist error if I put my templates in the inner my_project folder instead of the outer my_project folder which contains the manage.py file. Is there some config file that specifies the default location of the templates (and other files)?can you please help me with the command change if I put templates in the inner (sub) folder? I just began my first Django project and would appreciate your help.
Ok, django looks for your templates at default places. I recommend you put your html files there (as a beginner).
Default places are:
- my_site_project/
-- templates/
--- base.html
--- navbar.html
--- footer.html
-- my_app1/
--- templates/
---- my_app1/
----- index_app1.html
----- about_app1.html
-- manage.py
If you put a general templates/ folder inside your project, same folder as your manage.py, those templates will be pulled.
If you want app specific templates the convention is to put a folder inside your app, again called templates/. This can lead to overlap if you put index.html inside your app-templates and an index.html inside your general templates/. Therefore you need to namespace the templates of your app. Create the app; create a folder called templates/ inside it; inside just created templates/folder you create another folder called my_app1; in that folder create your .html files.
Read the tutorial here. It is probably easier to understand than my jibberish. It is the official django tutorial.
A part of it:
First, create a directory called templates in your polls directory.
Django will look for templates in there.
Your project’s TEMPLATES setting describes how Django will load and
render templates. The default settings file configures a
DjangoTemplates backend whose APP_DIRS option is set to True. By
convention DjangoTemplates looks for a “templates” subdirectory in
each of the INSTALLED_APPS.
You can, in your settings.py via DIRS, specify where django looks for your templates. Although as a beginner I do not know why you should not stick to the conventions first.

Should my Django site's main page be an app?

I have finished reading the Django official tutorial which teaches how to create a simple polls app, which is the way they chose to teach beginners the Django basics.
My question is, now that I know how to make a simple app and want to create my own website (using Django), should the main (front) page of my website, be an app as well? If so, how do people usually call it and configure it? I mean, it should do nothing but render a html template, so why make it so complicated? If not, where do I put all the static files and how do I reference them? I am a bit confused and could use your help. Maybe I misunderstood Django's main use?
You can create your templates and static files in the root project folder where your manage.py file lives. In the root folder create the following folders:
templates (for HTML)
static (for CSS, JS and images)
In your settings.py file, make these variables look like this:
TEMPLATES = [
{
...
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
...
},
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Note: STATICFILES_DIRS variable is initially not present in the settings.py file, but you can add that by your own. Django, by default, finds static files in the static directory of each app. If you want Django to read the static directory you created in the project root, you need to add this variable. Django official documentation reference: https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATICFILES_DIRS
To render these templates you can create views.py in the directory where your settings.py file lives and add the route in urls.py in the same folder.
This is one of the several ways to achieve what you want. Hope you won't need to plug these templates (eg, your home page) to or say use these templates in any other project, otherwise do as Timmy suggested in the comment on your post.
I usually start with 3 apps (call them whatever you want):
layout - basic layout: some common static files and a basic-template (other templates extend this one, main purpose is to
include a common html <head> ). Also contains the home-page and some other simple pages.
config - contains the project settings / configuration, and the main urls.py (and in my case also wsgi.py)
myapp - the actual app I want to create.
This nicely separates functionalities. Often I can just re-use the base app for other projects.
$ ./manage.py startproject config
$ ./manage.py startapp layout
$ ./manage.py startapp myapp

Changing Admin Templates - Django tutorial 2

This question was answered more or less here but it didn't work to me and as far I can see more people have the same problem.
In my settings.py I have this lines:
#TEMPLATE_DIRS = [os.path.join(BASE_DIR,'..', 'templates'),]
TEMPLATE_DIRS = ("/root/GODJANGO/thedjango/django_project",)
The comented line didn't work. It works if I write the full path but It's not professional and I don't wanna have problems when I three months later I migrate my server because I will not remember this thing.
Can anybody tell me how to write My Path correctly ("dynamically, I mean")
Please tell me where is the best directory to put my templates folder and also my admin templates folder
If you have the following project layout:
manage.py
myproject/
settings.py
urls.py
wsgi.py
templates/
admin/
app1/
app2/
Then you can dynamically set your template directory by putting the following in your settings.py:
...
SETTINGS_PATH = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, "templates")
)
...
if your template folder is in the parent folder to the settings.py you will need something like:
...
SETTINGS_PATH = os.path.abspath(os.path.dirname(__file__))
PROJECT_FOLDER = (os.path.split(SETTINGS_PATH))[0] # get the parent directory
TEMPLATE_DIRS = (
os.path.join(PROJECT_FOLDER, "templates")
)
...
As you can see, we are manually traversing the file tree to find where the templates folder is and assigning it dynamically.
The best place for your templates folder depends on your project layout (< 1.4 or >= 1.4) but it would be probably safest to say that it should be alongside your settings.py file. Your admin template folder will go inside your base templates folder: templates/admin/.
What are you trying to do with this paramater ".." in the commented line?
if it is means parent directory you can use os.path.dirname(BASE_DIR)
Second part:
It depends you or team work with; i love to keep templates directory in every app's directory (and if you do like this you don't need to define TEMPLATE_DIRS). I mean if i have templates of news app, they goes ../news/templates/. But this time my friend (front-end developer) says i cant find them, can we put all of them in one place?
so i put them in one directory with sub directories (../templates/news/). This main templates directory is in main project directory (near the manage.py file). And if you add this main directory to INSTALLED_APPS (because its kind an app) you don't need to define TEMPLATE_DIRS too. And even you can create models.py admin.py files here.
Considering the second part of your question and according to this the safest part for your templates is to create a templates dir under your projects main directory (e.g. blog).
As for the first part i am not sure.

Order of finding static files and templates in django-apps

For example I have 2 apps in my django project with templates and static files with identical subpath:
app1 /
static /
style.css
templates /
index.html
app2 /
static /
style.css
templates /
index.html
than, in settings.py I added this two apps:
INSTALLED_APPS = (
'app1',
'app2',
)
now I use in some way 'style.css' and 'index.html' in templates, e.g.
{% include 'index.html' %}
so, question is:
Does Django guarantee that when I reference to 'style.css' or 'index.html' will be used files from app2 subdirectories?
Is it any other way to point Django preferable variants of files in such situation?
As per documentation, first match wins:
Duplicate file names are by default resolved in a similar way to how
template resolution works: the file that is first found in one of the
specified locations will be used.
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-collectstatic
Now, when you tell Django to collect static files or render templates, Django asks special "finders" in order they are defined in your configuraiton for specified resource.
Default order for static files is "FileSystemFinder" that searches STATICFILES_DIRS in order they are added in it. If FileSystemFinder fails to find file in those dirs, Django uses next finder set, the "AppDirectoriesFinder" that searches in "static" subdirectories in your apps directories.
Same mechanic is applied to templates. When you tell Django to render "index.html", it first asks "filesystem.Loader" find template named like that in directories defined in TEMPLATE_DIRS. If search fails, Django asks next template loader, "app_directories.Loader" that searches template dirs in applications "templates" subdirs.
To answer your question, because app1 is registered before app2, Django will use it's style.css and index.html instead of ones coming from app2. If you want to change this behaviour, put app2 above app1 in your installed apps setting.
Documentation:
https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.loaders.app_directories.Loader

Django 1.4 (MEDIA_ROOT, STATIC_ROOT, TEMPLATE_DIRS)

I have a Django 1.3 project with this options in settings.py
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates'), )
But in Django 1.4 by default settings.py is moved in subdirectory with name that is equal to project name. Because of that static, media and templates directories now have to be moved in the same subdirectory?
Is this what I have to do, or just change STATIC_ROOT, MEDIA_ROOT and TEMPLATE_DIRS options?
I know that both variants are OK, but what is best practice for this in Django 1.4?
And also I know that every app can have it's own templates and static directories.
And is it better to put all other application directories inside the same subdirectory? This is not what is happening by default using manage.py startapp
OK the scheme that I follow is this:
myproject/requirements.txt - pip installable packages
myproject/deployment - Deployment stuff like server config files, fixtures(dummy data), etc.
myproject/docs - project's docs
myproject/tests - project's tests
myproject/myproject - project's operational code(and settings.py, urls.py)
Expanding myproject/myproject folder:
myproject/myproject/app1 - a regular app(encompassing its specific templates/static files)
myproject/myproject/app2 - another regular app(same as above)
myproject/myproject/website - semi special app, by convention.
This website app houses basically 4 things:
1) an empty models.py(so that django will consider it as a valid app)
2) a views.py with the entry point index view. Maybe some other views that don't fit in any other specific app.
3) a management dir with custom django commands which apply to the whole project.
4) a templates dir that has the 404.html and 505.html. Also it has a subdir called website that includes universal/base html templates that every other app extends, plus
the index.html.
5) a static dir with subsequent subdirs named css, js and media for global static files.
Nothing exotic I guess. I think that most people follow a similar pattern, but I would like to here any inefficiencies with this, if any.
EDIT:
with regards to settings for production VS development I use
the popular settings_local pattern, which you can read here and eventually will
lead you here, which describes a better pattern.