django message files work in project folder but doesn't work in app folder - django

I am testing django system and I started a project and a app for it. I have a problem with i18n system.
I have used _() function in app's view.py.
I put the 'django.middleware.locale.LocaleMiddleware' line in the MIDDLEWARE_CLASSES in project's settings.py file.
I run django-admin.py makemessages -l tr in app folder.
I run django-admin.py compilemessages in app folder.
When I run the service it didn't work. After I search in here and some other places I see the way how django looking these message files. First it looks app's folder, after project's folder. There are another places as well.
I moved my message files in project's folder and it worked. I moved the locale folder in app's folder again and it didn't work again.
I need to understand why. I want every translations, message files in releated app.
Regards,
I found the problem, I forgot to add the app name to INSTALLED_APPS settings. When I added, it works.

I tested it for myself and found it working.
Are you sure you use it in code in right way?
app/views.py:
from django.utils.translation import ugettext as _
from django.contrib import messages
...some code here...
if saved_changes:
messages.success(request, _('Item \'%s\' was saved.') % item.name)

Related

PyCharm cannot resolve reference in __init__.py with Django project apps

I am at my wits end with this issue, and would love some help resolving this.
I have a Django project with a bunch of sub apps as such:
my_project/
manage.py
my_project/
settings.py
urls.py
wsgi.py
app_root/
__init__.py
app1/
__init__.py
models.py
views.py
urls.py
templates/
[various templates].html
app2/
__init__.py
models.py
[etc]
app3/
[etc]
in my django settings.py i have installed apps as such:
app_root.app1,
app_root.app2,
In PyCharm, I've tried various things but essentially have Content Root as the top "my_project/" and app_root, app1, app2, etc as Source Roots. I've tried just having app_root as the only Source Root, and I've tried having only app1, app2, etc only as Source Roots, but nothing makes any difference.
Everything functions fine. app runs and everything. However, PyCharm has an inability to resolve my apps.
However, if i try this:
import app_root
...
def some_function(self):
app_root.app1.models.My_Model.objects.all()
it will highlight app1 with the error "Cannot find reference 'app1' in '__init__.py'"
This also means it can't do autocomplete anywhere in the path while doing app_root.app1. - it has no idea about models, views, etc. despite having an (empty) __init__.py in every directory.
I also cannot use any refactoring because it always says "Function is not under the source root"
I've spent countless hours trying to get PyCharm to behave but simply cannot find a way to do it. Is there any way this can be done so PyCharm will autocomplete my apps and not keep giving inspection warnings?
I had some similar issues. My solution; within the PyCharm preferences I added a path to app_root in my active Python Interpreter.
After an exchange with the PyCharm folks, here is what I learned:
Django imports all apps in INSTALLED_APPS variable and their models using __import__ for its own purposes.
In your case, it runs
__import__("app_root.app1")
__import__("app_root.app1.models")
After that, you call import app_root and obtain module app_root with app_root.app1 and app_root.app1.models already imported by internal Django code
Fact that Django imports apps and models is Django internals, it is undocumented and may be changed in future releases. We believe you should not rely on it in your production code, nor PyCharm should.
Here is example in bare python (no django):
__import__("encodings.ascii")
import encodings
print (encodings.ascii.Codec) # this code works, but PyCharm marks "ascii" as "unknown module"
So basically, it's not supposed to work as import app_root, but Django funkiness is masking that.

Django custom commands not showing up on Heroku

I’m having a problem when using django custom commands on Heroku.
On my local machine, the custom command appears in help if I run ./manage.py help and running ./manage.py deletedphotos runs it too.
All the init.py files are there and the settings are also correct, i.e. there are not major config differences between my local and Heroku instances.
Now, when I put it on Heroku, it does not show up. All my other non-default commands are there: ping_google that comes from installing sitemap.xml support and commands for south migrations. But for some reason, my self written commands do not show up.
I’ve also sent a support request to Heroku, but haven’t heard back from them in a few days, so I thought I’d post here as well, maybe someone has had any similar problems.
The deletedphotos.py file contents are pretty much like this if that matters anything:
from django.core.management.base import BaseCommand, CommandError
from foo.app.models import *
class Command(BaseCommand):
help = 'Delete photos from S3'
def handle(self, *args, **options):
deleted_photos = Photo.objects.filter(deleted=True).exclude(large='', small='', thumb='')
self.stdout.write('Found %s photos\n' % str(len(deleted_photos)))
I’ve tried checking all the correct python paths etc, but not 100% if I’m not missing something obvious.
I was actually able to find a solution. The INSTALLED_APPS had my local app referenced, but for some reason it was not working as intended.
My app is in: /name/appname/ and having 'name.appname' in INSTALLED_APPS was working fine in local setup.
Yet, on Heroku, I had to change the reference to just 'appname' in INSTALLED_APPS and all started working magically.
Your home directory needs to be on your Python path. A quick and unobtrusive way to accomplish that is to add it to the PYTHONPATH environment variable (which is generally /app on the Heroku Cedar stack).
Add it via the heroku config command:
$ heroku config:add PYTHONPATH=/app
That should do it! For more details:
http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/
I had this problem too, found the answer here: Django Management Command ImportError
I was missing an __init__.py file in my management folder. After adding it everything worked fine.
Example:
qsl/
__init__.py
models.py
management/
__init__.py
commands/
__init__.py
news.py
jobs/
__init__.py
news.py
tests.py
views.py

Django syncdb can't find my apps

I'm a django newbie and have been having a problem.
In my project root I created a folder called 'local_apps' and within it I put the app 'myapp'. I updated the INSTALLED_APPS within settings.py with: myproject.local_apps.myapp
However when I try to syncbd, Django gives an error: 'no module named local_apps.myapp exists'
When I put 'myapp' back in the project root, it works again but I dont want it that way. I want to keep my apps in the folder 'local_apps'.
Can you tell me what I am doing wrong here.
Thanks in advance.
I think that's a generic python error, not a Django error.
Maybe you need to create an __init__.py file in the local_apps directory, so python knows it's a module.
Do you have a models.py within the myapp folder? I think Django may need to see that file in the app.
[Edit: actually, I think wisty above may be right, make sure you have an __init__.py, if you do, try the models.py.]
I was facing the error: ImportError: No module named foo
To solve this, I just added the Django site package to the Python path at settings.py module like this:
PKG_DIR = os.path.dirname(__file__)
sys.path.append(PKG_DIR)

How do I include a Django app in my PYTHONPATH?

I want to be able to import things from applications in my project without referring to my project name.
My folder structure is like so; I have a project called djangoproject1, and inside I have a folder called apps, and then I have my individual apps in that folder.
djangoproject1, apps, and all my applications have an empty "__init__.py" file in them.
In my settings.py file I have the following code:
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
I've tried adding my apps to INSTALLED_APPS several ways:
'djangoproject1.apps.app1',
'djangoproject1.apps.app2',
or
'apps.app1',
'apps.app2',
or
'app1',
'app2',
but nothing works. Whenever I try to do:
from app1 import *
I got an unresolved import error. I'm using the latest versions of eclipse and django
Ok, so I got it to work by adding the apps folder to the PYTHONPATH through eclipse under Project Properties. Is this eclipse only though? I'm not sure how this will work when I try and deploy the site. What do you guys think?
The statement sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps")) looks OK to me. Following this you only need to add app1 to INSTALLED_APPS and everything should work. But apparently they are not working.
Try the following: 1. Print your sys.path and verify that your project's app directory is in the list. 2. Double check that your have an __init__.py inside your apps folder.
Update
OK. Now can you fire up Django shell and try importing again? If it fails, please post the stack trace here.
You must include apps directory to PYTHONPATH. Be sure that in apps directory don't have __init__.py, because it become a package instead of "simple directory". Than, include in settings.py
app1, app2, app3
Use
from app1 import *

Setting up Django on Server with WSGI getting Import Error "Module named myproject.urls Not found"

So I have created a django site and I wanted to move it from my computer to my server. I set up Django on the server, and used the WSGI configuration. When I try to go to the home page I get an Import Error, It says that the module "myproject.urls" isn't found. It's a Django error, and it looks like it is getting the settings.py file and looking at the setting for ROOT_URLCONF and seeing the right urls file. I created this project with the usual django-admin.py startproject myproject and I just wanted to see if everything was configured correctly, but now I'm getting this error.
Any Suggestions?
Remove the "myproject" from "myproject.urls". Somehow WSGI addresses the settings as the root, so no need to refer to it again.
It sounds like myproject isn't on your path - what happens if you load up a python shell and run import myproject? If that works, what happens when you run import myproject.urls? If only the second import fails, there's a syntax error in your urls.py or one of the files it imports.
#Afrowave, you saved a huge headache - thank you from me too!
Further to this - I did a little more digging and wanted to avoid having to amend a dozen files in my app to account for loosing 'myproject.' and the start of every import.
Instead, I found if you do something like this -- you don't have to :)
ROOT = '/home/user/path_to_project_root' # In my case, also the dir that contains media, templates etc
APP_ROOT = '/home/user/path_to_django_project'
sys.path.append(ROOT)
sys.path.append(APP_ROOT)
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Hope this helps someone in future.