I have a django app that I developed in the 1.2 days. I'm now trying to port it over to the 1.4 project format.
The old way my project was set up was as follows:
django_project/
settings.py
manage.py
urls.py
app1/
app2/
app3/
I'm changing it to use the new manage.py and my directories look like this:
django_project/
manage.py
project
urls.py
wsgi.py
app1/
app2/
app3/
The problem is that all over my code I import stuff like this:
from app1.models import SomeModel
which now gives me an import error. Doing this fixes it:
from project.app1.models import SomeModel
I really don't want to have to go all through my project to change all those imports. Is there something I'm missing? Is there an easier way? Or is this how you're supposed to do it?
You should not put your apps into the project module. Django's startapp puts them in the project root, as it was before. project module is a place for project-wide settings, urls and such stuff only. Your apps should stay outside, in project root.
You can keep your current layout, since it will works fine. For new projects I think you can start to put your apps inside the 'project' module. If you check the 1.4 Release Notes you'll see it's the recommended layout. But if you are developing generics apps (that you can use in more than one project) probably the better place is project root.
Related
I have a django 1.11 project with some rest_framework related apps. Writing tests for new app, I have suddenly gotten the issue
'RuntimeError: Model class core.myApp.models.query_record
doesn't declare an explicit app_label and isn't in an
application in INSTALLED_APPS'
I do have this listed in installed_apps, and in the end, the reason I have this issue is because I have an __init.py__ file in the top level of the project that loads some config for some related celery tasks.
I'm unaware why I haven't seen this issue in other app tests, as there is nothing particularly special about this app or it's model. But, this is causing all tests to fail for this app.
So, my question is, is there a way I can run these unit tests and ignore the projects top level __init.py__ ? Or maybe I should ask, is there a non-hacky way to do it?
the project level __init.py__:
from __future__ import absolute_import
from .celeryapp import app as celery_app
All other app init.py files are empty.
A problem might be, that .celeryapp is trying to import some models that aren't loaded yet.
You can try to add a AppConfig to core.myApp and load/import your celery app in it's ready() method. See the Django docs for more information Django docs for more information
It turned out, in the end this was simply because of how I was running tests
I was running tests like this
./manage.py test myApp --pattern=*.py
The pure wildcard was causing import issues. I should have done this:
./manage.py test myApp --pattern=prefix_*.py
D'oh.
I just started testing out PyCharm on my existing Django project, and it doesn't recognize any imports from apps within my project:
in my_app1/models.py:
from my_app2.models import thing
"Unresolved reference 'my_app2'"
Why is this? My project's directory structure matches the recommended layout, and it runs without errors, it's just PyCharm's magic doesn't want to work on it.
It seems related to this question:
Import app in django project
But I can't figure out what I am doing wrong. If I try:
from ..my_app2.models import thing
The PyCharm error goes away and it can auto predict, etc. But when I run the project Django throws:
ValueError: attempted relative import beyond top-level package
EDIT:
Project structure:
my_project/
src/
manage.py
db.sqlite3
my_app1/
templates/
__init.py__
admin.py
models.py
urls.py
views.py
...
my_app2/
templates/
__init.py__
admin.py
models.py
urls.py
views.py
...
my_project_app/
settings/
__init.py__
urls.py
...
I was having this issue using a "2 Scoops of Django" project layout, e.g.
/project_root
/project_dir
/config
/settings
/my_app
/tests
models.py
/requirements
readme.rst
The code was working, but in /tests, IntelliJ/PyCharm showed an unresolved reference:
from my_app.models import Something
I had all the __init__.py files in place. I ended up having to set the sources root to project_dir:
Right-click on project_dir, Mark Directory as > Sources Root
Now that I can take a look over you project structure I can tell you that the problem appears to be related to a missing __init__.py in your 'src' folder. Try adding an empty file named __init__.py in the root of 'src' folder.
Also, take a look to this question, I think is the same problem or a very similar one.
Hope this could be useful, cheers!
I was having this issue after I change my environment to virtualenv, so I changed my python interpreter to my current virtualenv.
Go to File > Settings > Project Interpreter.
In that window you would be able to see all packages includes on this interpreter, Django should be there.
This worked for me.
Link about: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206598665-Unresolved-Reference-Errors-for-django
I have a python application (which we'll call app) I'd like to have a web front for. The application has many files and it's important the folder tree structure stays the way it is.
I've built a Django project and put it in a folder named "Web" in the app's folder, so now the folder tree looks like so:
[Data]
[Resources]
[Web]
[WebFront]
normal django app files
[Web]
__init__.py
settings.py
urls.py
wsgi.py
__init__.py
manage.py
main.py
Here's the code on the app's main.py:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.Web.settings")
django.setup()
This code causes an exception on the django.setup() line as (I think) django does not find the project modules: ImportError: No module named WebFront (WebFront is the name of the django app)
I suspect this is caused because django runs in the directory of python app, and therefore cannot find the folder WebFront - Which should actually be Web/WebFront
Can this be done? Or should I reverse the order and put the python app in the django app?
This is not a duplicate of the following questions as the folder nesting causes a different problem (I think)
Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
Easiest way to write a Python program with access to Django database functionality
Using only the DB part of Django
You can locate your main.py script where you like. However, if it is outside of the Web folder, then you will have to add Web to the Python path, otherwise imports like import Webfront are going to fail.
import sys
sys.path.append('/path/to/Web/')
Once you have done that, you can change the DJANGO_SETTINGS_MODULE to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.settings")
I have a django application structured like this...
app_foo
__init__.py
urls.py
views.py
models.py
bar_app
__init__.py
...
bar_app...
By using distutils, I can get the application to install into the python path under the "app_foo" module name.
However, any of the code inside of the "bar_app" python files which refers to things inside the django app relatively does not work when executed from the python path. For example,
from bar_app.views import stuff
I know that I can go through the app and change all the references to be absolute. For example,
from app_foo.bar_app.views import stuff
My question:
Is there anyway I can get all of the apps inside "app_foo" to also be on the python path?
Conceptually this would be similar to saying from app_foo import * for the entire path.
You can do
from .bar_app.views import stuff
http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
I am trying to include the following Tag In Google App Engine Web Application:
http://www.djangosnippets.org/snippets/1357/
Is there any configuration of this file to make it work with Google App Engine?
Cause I followed the Django Template tutorials: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
and have this structure:
templatetags/
__init__.py
range_template.py
in the Template file, I have {%load range_template%}
But I am getting the error:
TemplateSyntaxError: 'range_template' is not a valid tag library: Could not load template library from django.templatetags.range_template, No module named range_template
The other thing that might be a problem why this ain't working is, the INSTALL_APPS settings.py file. Not sure how to configure it.
I have a settings.py file in the root of my application and included this:
INSTALLED_APPS = ('templatetags')
Any advice would be greatly appreciated.
try doing the following:
$ python ./manage.py startapp foo
Add foo to installed apps:
INSTALLED_APPS += ('foo',)
And move your templatetags directory into your foo app. Something like:
./djangoproject
__init__.py
settings.py
urls.py
etc..
foo/
__init__.py
templatetags/
__init__.py
range_template.py
Django convention is that template tag code resides in apps, in directories named templatetags (see docs). I assume the same would be true for GAE.
In case someone searches for this, I wrote a small article in 2008 about this: http://daily.profeth.de/2008/04/using-custom-django-template-helpers.html
Please make sure to restart the development server after following the above step