manage.py - not in project folder? - django

I'm trying out Django for the first time, and I'm trying to follow the tutorial provided by the django team.
After I've created a new project I get the following folder/file structure, just as the tutorial says I should:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
When I create an app I run:
python manage.py startapp polls
...which creates the app 'polls' in the same folder as the manage.py file - which gives me:
mysite/
manage.py
polls/
__init__.py
admin.py
models.py
tests.py
views.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
This means the app I created gets created outside my project folder, which, of course, should not be the case. I have tried to move manage.py inside the project folder. But when I do that and run:
python manage.py syncdb
...I get the following error:
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'testproject.settings' (Is it on sys.path?): No module named testproject.settings
I guess I could move the app manually to the project folder, but this is something I shouldn't have to do.
So, either something is wrong in the tutorial (which I have very hard to believe), or I'm missing something out here (more likely).
Thanks in advance.

This is the new proper layout. "mysite/mysite" is an app, and "mysite/polls" is an app. The "mysite" parent folder is your project folder.

#holyredbeard that is the correct layout are you reading the older documentation?
Useful reading: http://www.tdd-django-tutorial.com/blog/articles/2012/tutorials-updated-django-14-and-its-weird-new-fold/
Don't move the manage.py it should sit outside the apps and the project folder.
since 1.4 common layout example...
project_root/
project_name/
media/
static/
static_root/ (in production)
templates/some_app/foo.html (overriding some_app at project level)
/admin/some_app/some_model/change_list.html
(overriding admin changelist for some_app.models.some_model)
settings.py
settings_deployment.py
urls.py
some_app/
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py

This is the official layout since version 1.4.
The rationale behind is explained well in the release notes:
https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
Do not move manage.py. In general you can expect that Django's own scripts always do the right thing, you never need to move any files just to get it working.

Related

Pycharm not showing all files from django external library

In pycharm under external libraries I'm not able to see all files (e.g all templates are missing).
The external librarys are coming from a remote server (docker).
On the server itself, I'm able to see every file.
Pycharm
Docker container
root#fcb645e62162:/usr/local/lib/python3.5/site-packages# ls django/contrib/admin
__init__.py actions.py checks.py exceptions.py forms.py locale models.py sites.py templates tests.py views
__pycache__ apps.py decorators.py filters.py helpers.py migrations options.py static templatetags utils.py widgets.py
This should work in the latest PyCharm build, but maybe there's something
special in your case that breaks it.
Try update version.
or go to Settings | Project Structure and add the folder with
your Django installation as a content root to your project.

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 can't load app

I'm using django 1.4, after i created the project using django-admin, I create an app, add it to settings.py in INSTALLED_APPS but then when I do manage.py runserver I get Error: no module named myapp
Does anyone know why?
Make sure you have two things:
correct path. You should be able to do ./manage.py shell and then import 'myapp' if not try 'myproject.myapp'
models.py in your app folder and __init__.py

project layout and project utilities/libraries?

I am a bit confused with the project layout created since Django 1.4.
I want to add some global utilities to be used through my whole project and apps.
I tried to add a utils package at the same level of my apps but Django does not see it.
mysite/
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
utils/
__init__.py
shortcuts.py
Any advice / best practice on how to use such a global utilities package ?
Thanks
Michael
Instead of adding it to PYTHONPATH which you have to do on each machine you install your project on, you can consider adding it to the django settings (mysite/settings.py) in INSTALLED_APPS variable:
INSTALLED_APPS=(myapp, utils)
This has the effect of adding utils to the python path, so you can do things like:
from utils import shortcuts

Django 1.4 project wide code ( template tags)

With 1.4 round the corner I thought I would give it a go but I have a question about the new project layout.
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
I can see the logic here and I am a fan of the new layout however if I am having issues with adding project wide code.
For example:
I have created a new templatetag that is specific to this project and doesn't fit logically fit in any one of my apps. To me this should then fit within mysite however (as far as I can tell) this is not then processed. apptag.py (see below) is available but sitetag.py is not. Now, I am assuming this is because mysite not processed in the same way as an app(?).
manage.py
myapp/
templatetags
__init__.py
apptag.py
__init__.py
models.py
mysite/
templatetags
__init__.py
site.py
__init__.py
settings.py
urls.py
My question then is what is the right way to go about this issue? Should I create an app called 'core', 'mysitecore' or such like? I can add 'mysite' to INSTALLED_APPS but that just feels down right wrong. Or is there another option that I am missing here.
Thanks in advance.
Templatetags need to be inside an app that is included in INSTALLED_APPS. This is, in my opinion, a wart in Django that isn't fixed by the new layout. Your idea of using a core app - or, as I often do, utils - is the right one.