What is the basic difference between the django.admin and manage.py?
Also, What is the meaning of the sentence, "manage.py acts as a "thin wrapper
around django.py" ?
here is the documentation doc that outlines the difference between django-admin and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks. This document outlines all it can do.
In addition, manage.py is automatically created in each Django
project. manage.py is a thin wrapper around django-admin.py that takes
care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Here's more documentation explaining the difference:
https://django.readthedocs.org/en/1.4/ref/django-admin.html
https://docs.djangoproject.com/en/1.8/ref/django-admin/
To add what I said on the comments above - manage.py is needed because every app name will be different and you have to specify your app's settings to run your django app successfully. There's really no difference between django-admin and manage.py except this one line
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[App name].settings")
everything that you type after manage.py, django-admin.py takes care of as shown:
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Sometimes, it helps just looking at the source code to see what is really going on if the documentation doesn't help.
Related
I don't know how to use gunicorn with django. Could you give me some help?
This is how I run the server with django. It is https
python3 manage.py runsslserver xx.8x.x3.x4:443 --certificate /etc/letsencrypt/live/callservicesvps.online/fullchain.pem --key /etc/letsencrypt/live/callserv.com/privkey.pem
And in the gunicorn documentation it is mentioned that it must be executed as follows:
gunicorn myproject.wsgi
And here I have 2 questions. What is myproject.wsgi? Where Can I find it? Because if I look in the directory where the django project is, the only thing I find with wsgi is a file called wsgi.py
Running the server as follows gives me an error
gunicorn /home/proyectdirectory/wsgi.py
It also gives me an error if I put:
gunicorn /home/proyectdirectory/wsgi:Some_directory_where_the_proyec_is
What is myproject.wsgi?
myproject.wsgi IS the wsgi.py file you have located inside your project.
[End of answer]
But a further explanation will clear up why this is..
I imagine most people will look atmyproject.wsgi and see a file with an extension file type .wsgi but this is just because of the way importing of modules is written in python.
I want to clarify what a module and a package before continuing an explanation.
What is a Module
From the jargon heavy python docs
A module is a file containing Python definitions and statements.
Put simply, a module in python is just a python file containing any sort of functions, variables, or classes etc.
What is a Package
A package is just a collection of modules. The most simplest example, a special directory containing python files. In order to tell python that a directory is a package it must have a file named __init__.py inside of it. You will find a few of these inside different directories inside your django project. This is why they are there.
Now, I can say what I want to say which is the structure of the module namespace in python.
package.subpackage.module
If you look inside your wsgi.py file you'll see a good example of importing from django's own wsgi module.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')
application = get_wsgi_application()
More specifically
from django.core.wsgi import get_wsgi_application
If you look inside the django package you can see how this works.
django (package) core (subpackage) wsgi (module)
Remember, the command gunicorn myproject.wsgi should be run inside the base directory of your django project. I always remember this as the directory containing the manage.py file. That is how gunicorn can find the wsgi.py file using the module namespace in this way. gunicorn is written in python afterall.
gunicorn /home/proyectdirectory/wsgi.py will error because python module imports don't contain / and even if you tried gunicorn home.proyectdirectory.wsgi home and proyectdirectory are not python packages.
Now hopefully this makes sense:
"If gunicorn myproject.wsgi is referring to the wsgi.py file why not just put gunicorn myproject.wsgi.py?"
You can't put a .py extension because this will refer to a module inside the wsgi subpackage with a filename py.py!
Several configuration files exist.
If these files have different names
How do I change the settings file every time I run this command?
"python manage.py runserver"
Its so simple
read Main Django Tutorial, its all about setting django configuration
a shortcut for using in runserver command is --settings= and this also works with uwsgi
but if you intend to change setting without re-running the server django-constance is the answer
you can add to manage.py file
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings_name')
After that run
py manage.py ----
I have a Django project. Everytime I deploy, I need to run a series of manage.py command (such as syncdb, south migiration, fixture update).
I am getting tired of type the command line by line and therefore I wrote a python script to do these:
import subprocess
subprocess.call(['python', 'manage.py', 'syncdb'])
#Skip the detail
subprocess.call(['python', 'manage.py', 'loaddata', 'setup/fixture.xml'])
I am wondering if there is a better way to do this?
Thanks.
You can use fabric, a Python library that allows you to script remote actions. This question has some links in the accepted answer for more information on fabric and django.
You can also call management commands directly:
from django.core.management import call_command
call_command('syncdb')
call_command('loaddata', 'setup/fixture.xml')
Save that as a normal python file and execute it from your shell or as part of your deployment scripts.
I can't sign in to Django admin with any of the superusers I've created. Tried creating new superusers, changing passwords, etc. - no error messages for any of these processes, but still can't sign in.
I'm not sure if it's related, but I also can't run django-admin.py commands. Here is what I've done so far:
$ django-admin.py validate
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
$ export DJANGO_SETTINGS_MODULE=mysite.settings
$ django-admin.py validate
Error: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings
sys.path shows ['/Users/joerobinson/Sites/django/mysite' ... (other stuff on path)]
Is there something else I need to do to register the mysite module?
python manage.py runserver works fine.
Conclusion
For the question about logging in to django admin, I did not have django.contrib.auth.backends.ModelBackend included in my AUTHENTICATION_BACKENDS - adding this allowed me to sign in to admin.
I'm still working on the django-admin.py configuration question (which appears to not be related), and will reopen it in a separate question.
In my case, I had "SESSION_COOKIE_SECURE = True" because I was trying SSL configurations. Just commenting this line worked.
Well, to answer one of your questions, the parent folder of mysite is what needs to be on the python path. Adding mysite itself to your python path will expose the contents of mysite, not the mysite module to python.
As for runserver working / passwords not working, I haven't a clue. Have you customized anything with authentication?
Can you start the shell and try authenticating with your superuser?
http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate
You said in a comment that "is_staff was set appropriately... same problem." -- does that mean you were able to log in with the first superuser to set is_staff??
I ran into a similair problem but, I couldn't login on the admin interface even after removing the database and creating it from scratch. The problem was caused by a real silly mistake.
Don't run python manage.py testserver by accident. Make sure you use python manage.py runserver.
I exported the path of my django project by
$ export DJANGO_SETTINGS_MODULE=/Users/masi/Documents/Test/djangobook/ch3.settings
I run unsuccessfully
$ django-admin.py runserver
Error: Could not import settings '/Users/masi/Documents/Test/djangobook/ch3.settings' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.
How can you start Django server without the error message?
Your $DJANGO_SETTINGS_MODULE should just be set to ch3.settings. Just make sure that the ch3 app is in your $PYTHONPATH, too.
For example, if your app is at /Users/masi/Documents/Test/djangobook/, then set $DJANGO_SETTINGS_MODULE to ch3.settings, and make sure your $PYTHONPATH includes /Users/masi/Documents/Test/djangobook.
$ export PYTHONPATH=/Users/masi/Documents/Test/djangobook/
$ export DJANGO_SETTINGS_MODULE=ch3.settings
From the django docs on django-admin.py and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks.
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Generally, when working on a single Django project, it’s easier to use manage.py
So, if your directory structure looks like:
djangobook/
ch3/
settings.py
Do the following and you can ignore all DJANGO environment variables (unless you have some really weird install):
$ cd /Users/masi/Documents/Test/djangobook/ch3/
$ python manage.py runserver
For those that come across the same error, when trying to run something similar:
python manage.py runserver --settings=settings_dev
When the settings file is within an app directory, like so:
mysite/
settings.py
settings_dev.py
requirements.txt
manage.py
You don't have to specify $PYTHONPATH (at least not four years on) you just need to make sure your --settings value contains the folder name — you also need to use dot notation, slashes will not do.
python manage.py runserver --settings=mysite.settings_dev
It is the same story when exporting a $DJANGO_SETTINGS_MODULE value:
export DJANGO_SETTINGS_MODULE=mysite.settings_dev
Might save someone else the time that I lost working that out.
You can also try manage.py.
From your project directory, run
$ python manage.py runserver
Even though it's just a wrapper, manage.py always works for me while django-admin.py doesn't. Obviously we're both doing something wrong (I just got started with Django), but this should get you going at least.