What does INSTALLED_APPS setting in Django actually do? - python

What does this actually do? I recently branched out my project from 1 app into 6 different apps and forgot to update the INSTALLED_APPS part of my settings file. Everything still works even though I didn't list the new apps in. Is that supposed to happen? Do I need to include all my apps in INSTALLED_APPS?

yes.
INSTALLED_APPS helps django to sync the database, run tests, get the urls to work and more related issues.
Maybe your installed apps still works because the main one calls the others with imports, a django app is nothing more that a simple python module that is imported when called in the settings file, that's why you get a invalid syntax error after you run the development server because an import won't work with invalid syntax.

Related

In pycharm can I run every file for django?

I'm new to Django. My localhost site is running fine. Since I am using pycharm it is easy to run any file. I decided to run each file in my django project, and came across several errors, such as this one in my views.py:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Even though the site is running, what seems like properly, I'm seeing this message. What is causing this, and is it typical?
You cannot run each file present in your django project individual.
No matter those are file with .py extension. They depend on the django framework to get the project running.
The reason you might be seeing that error is because you might be using the attributes present in the settings.py file which in turn requires django to set the application running like starting the WSGI server, getting all the dependencies and the installed apps ready before you actually use anything.
Understand that Django is a Framework and it relies on many underlying components to actually work. Even thought you can technically run any file in any manner, you cannot start the application itself.
There are other ways to do it if you like to test the application like using django shell by python manage.py shell to check and test the application, which would be a better way of doing individual testing rather than running each file standalone.
You can run any individual python file in a Django Project with Django, but keep in mind that the settings for Django must be supplied. This is not a good practise to run individual file with Django but for debugging purposes you may use it (for example. to test a parser that you wrote with database access),
You have to configure the Django settings before you can do anything with Django on a single file.
from django.conf import settings
settings.configure()
def do_something_with_a_model():
# does something with a model
print "I am done!!"
Note that relative imports may break when running on single files.
(for example. imports like from .another_module import AnotherClass may break when working with single file.)

DJango INSTALLED_APPS

I am just curious, Is it okay to remove some of the default Installed Apps in DJango Settings? See image below.
These apps creates a new tables in my database and I don't know when can I use those tables.
Thank you!
You can, though if you're unsure the purpose of the app it would be best to leave it included.
In fact, the Django documentation mentions this in the very first tutorial:
Like we said above, the default applications are included for the common case, but not everybody needs them. If you don’t need any or all of them, feel free to comment-out or delete the appropriate line(s) from INSTALLED_APPS before running migrate. The migrate command will only run migrations for apps in INSTALLED_APPS.
https://docs.djangoproject.com/en/1.8/intro/tutorial01/

How to write custom django manage.py commands in multiple apps

Imagine I have two or more apps in my django project, I was able to successfully write and execute custom manage.py commands when I had only one app, A.
Now I have a new app, B, and as mentioned in https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ I created directory structure of B/manangement/commands and wrote a custom module.
When I run python manage.py , it keeps complaining Unknown command. However if I move this command to other app, i.e. to folder A/management/commands and then run python manage.py <command>, it works seamlessly.
Any idea how I can resolve this?
As #Babu said in the comments, It looks like you may not have added your app to INSTALLED_APPS in your settings.py.
It's also possible that you're missing the __init__.py files (that are required in python modules) from the management and commands folders.
Alternatively, (sorry to say this) you may have misspelt "management" or "commands", or even the name of the command you're running.
Most likely, you did not include app B in your settings.py
If you just run python manage.py with no command specified, it will print the list of commands Django can find.
This can help rule out misspelling the command name, but doesn't answer the question of whether or not you made management and commands both packages, or if app B simply isn't listed in your settings.INSTALLED_APPS
It looks like you have not registered your app B in INSTALLED_APPS in our settings.py.
You also need to add a __init__.py file in both folders(management and commands) to make it as a package. Make Sure you spell folder names correctly.

Uploading Django Development to Dreamhost/Passenger first time "Could no import"

I've been developing on my laptop.
Now, I've uploaded my development project from Django 1.2/Python 2.7 up to Dreamhost created using the Passenger setup. I am using South for migration.
I modified settings.py to access MySQL. Got Admin working.
Questions:
1) Where is the std out shown when you hit the site? (all my print statements)
2) I had to add
from decimal import Decimal
to get the settings.py to run.
Now, hitting the server gives me a 500. When I go to the commandline and try any manage.py command I'm getting:
Error: Could not import settings 'pholdershare.settings' (Is it on sys.path? Does it have syntax errors?): No module named settings
I modified the permissions for my folder to 755 but that didn't change anything.
3) Regarding development to testing to live, I'm totally unclear on how to do this. I want to have a nice solid system for development where I can do releases but I've never done that. I have been using GIT locally. Can anybody point me to the tutorial on setting up the release system?
Suggestions?
Thanks much!
"Could not import settings 'pholdershare.settings'"
...usually means either settings.py has an import error or syntax error, or pholdershare has not been added to sys.path in your wsgi.py

Django sub-applications & module structure

I am developing a Django application, which is a large system that requires multiple sub-applications to keep things neat. Therefore, I have a top level directory that is a Django app (as it has an empty models.py file), and multiple subdirectories, which are also applications in themselves.
The reason I have laid my application out in this way is because the sub-applications are separated, but they would never be used on their own, outside the parent application. It therefore makes no sense to distribute them separately.
When installing my application, the settings file has to include something like this:
INSTALLED_APPS = (
...
'myapp',
'myapp.subapp1',
'myapp.subapp2',
...
)
...which is obviously suboptimal. This also has the slightly nasty result of requiring that all the sub-applications are referred to by their "inner" name (i.e. subapp1, subapp2 etc.). For example, if I want to reset the database tables for subapp1, I have to type:
python manage.py reset subapp1
This is annoying, especially because I have a sub-app called core - which is likely to conflict with another application's name when my application is installed in a user's project.
Am I doing this completely wrongly, or is there away to force these "inner" apps to be referred to by their full name?
You are doing it the right way, since django itself does it that way. The admin app for instance is registered in INSTALLED_APPS as django.contrib.admin, but to reset it you have to use manage.py reset admin, and indeed, manage.py reset django.contrib.admin does not work.
It could be considered as a bug in django...
However, you should not be concerned by name conflicts, because you should always run django inside a virtualenv environment, isolated from the rest of the python installation. This is an immensely more powerful and flexible solution than running django on an ordinary python installation. More info, for instance, here: http://mathematism.com/2009/jul/30/presentation-pip-and-virtualenv/

Categories