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

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.

Related

Why i am not seeing a .py file but something else (trying to create Django project)

So I got advice in another question and they started talking about paths and .exe and that I should not put my projects in the scripts. So I added
C:python27\scripts to my path and nothing seemed to change except when I created a new project, and went into the GUI to look for the folder and found it, it just says Manage instead of manage.py. It says its a python folder.
I then took ..\scripts out of the path and created the project again and still got the same thing.
Whenever I did it the first time without changing anything, I did see a manage.py file.
So the tutorial I am working that introduces me to Django asked me to open the manage.py file, and when I try to of course it tells me it can't open the file because it does exist. So what do I do?
If I'm understanding you correctly, you're simply trying to create a django project. Since you didn't mention which tutorial you're using, I'll link this resource and recommend following it page for page. It's a great walk-through for learning how to create apps and it's broken into sessions you can do over a period of time:
To answer your question: when you create a django project with django-admin startproject mysite it will install the manage.py into the mysite directory. If that doesn't answer your question, try describing the precise steps you're taking to create a Django project including all commands you're using (if any).
It's also helpful to know if you're using a virtual environment.

Namespace django app names

I have tried searching around for this, but searches including "django", "app", and "namespace" all bring up topics mostly relating to URLConf, not what I want to know.
In Django settings, apps included in a project are listed in the INSTALLED_APPS setting. Most community-released apps are not "namespaced" in that the app name consists of only letters, numbers, and underscores. There are official apps like "django.contrib.sites" that are "namespaced" with dot-syntax. What I cannot seem to find is: how do I do this? For example, if I want to create an app like "mycompany.app". The startapp command of manage.py does not allow naming like this if I try ./manage.py "mycompany.app", complaining of invalid characters.
So what Python and/or Django magic am I missing to create and use apps in this way? Or is there some reason why doing this is discouraged, and I should only make apps like "mycompany_app"?
Create a python package mycompany (i.e. folder with init.py file inside), inside create package app.
There is also namespace term in django urls, can be noticed in calls like reverse('namespace:view-name'), here is the doc for these: https://docs.djangoproject.com/en/1.7/topics/http/urls/#url-namespaces

Is there any reason why a project has no settings.py file?

I have inherited a Django 1.4 project from an uncooperative developer and am having to find my way around and try and set up a dev environment.
One of the issues I am having is a result of the fact there is no settings.py file in the project. In the directory where I would expect to find it, there are files like settings_production.py, settings_base.py etc. but none called settings.py. So when I try
python manage.py runserver
it complains about this. I have renamed one of them to simply settings.py and I get a little further. But I am obviously missing something - is there a valid reason for this situation to arise and if so, is there something I need to do in my local setup to allow this to work?
This is perfectly fine and used quite often. Different environments, like your local development environment and the actual production environment, often require different settings. You can pass the settings module to use to your manage.py command
or set the DJANGO_SETTINGS_MODULE in your command line environment.
manage.py runserver --settings=import.path.to.settings
# or
export DJANGO_SETTINGS_MODULE='import.path.to.settings'

What does INSTALLED_APPS setting in Django actually do?

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.

Django: When doing an include of URL's, should I specify project.app or just app

When including an app's URL's in the project's urls.py, my coding partner does it this way:
('^stops/', include('stops.urls'))
However, Django documentation specifies the following syntax:
('^clients/', include('project_name.app_name.urls'))
His way has worked. Is there a reason to specify the project name at all?
It depends on your PYTHONPATH setting and the structure of your projects and apps.
We have many, many projects. Each with several apps. All are on our PYTHONPATH, so the project name is essential.
If you have only one project, and the top-level project directory is on your PYTHONPATH, then each app can be resolved separately and you can't use the project name.
It also depends upon if the app is inside your project, or a reusable one.
I have a fresh virtualenv for every project, and use a separate mercurial repository for each app. These are then installed into the system path (either in editable form for development, or in non-editable form for deployment), meaning I have <appname> on the PYTHONPATH.
if on the shell you run
import this
you will see that there is a zen of python ''explicit is better than implicit'' hence thats the reason to specify the project name.

Categories