gopalraha
By:
gopalraha

How to run this command in wsgi.py file in django?

July 30, 2016 2.5k views
Django Python Frameworks CentOS

How to run this command in wsgi.py file in django

My wsgi.py files looks like

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

My Command that i want to add in wsgi.py file

python manage.py runserver 127.0.0.1:8000

I want to run this command using wsgi.py file how to add this command into it.

Thanks :)

2 Answers

If you want wsgi.py to execute the command python manage.py runserver 127.0.0.1:8000 as a shell command, you can use something like this:

import os
from subprocess import call
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
call('python manage.py runserver 127.0.0.1:8000')

But I wouldn't recommend doing it this way, If you are using apache or nginx to serve the django site and if you need to access it locally, you can make your website listen on *:80.

Use nano.

So the command would be:

nano wsgi.py

Make sure you call the command in the directory the wsgi.py file is in.

Once the file is open, add the line to the end of the file. Then save with Ctrl-o, hit Enter to save the filename, and exit out of nano with Ctrl-x.

You can also append the line to the end of the file:

echo "python manage.py runserver 127.0.0.1:8000" >> wsgi.py

Then from the command line run

python3 wsgi.py

or

python wsgi.py

...depending on your Python versions and settings.

Have another answer? Share your knowledge.