1

Python/Django newbie here!

In my new project, I am able to load the django template pages and the admin section, as well as update and delete things from the admin... I've been able to collectstatic without a problem and I can also run all commands with manage.py from bash, all from inside virtualenv.

Where I am stuck is trying to run a "python manage.py check" from the app's views.py via subprocess, for example:

some_app/views.py

from django.shortcuts import render
from django.http import HttpResponse
import subprocess
import shlex

def home(request):
    cmd = 'python manage.py check'
    subprocess.Popen(shlex.split(cmd))
    return HttpResponse("<html><body>Hello World</body></html>")

The HTML "Hello World" loads fine, the subprocess command results in Apache error:

"python: can't open file 'manage.py': [Errno 2] No such file or directory".

"python: can't open file '../myweb/manage.py': [Errno 2] No such file or directory".

I am not sure I know why this is, here's the file structure I have:

  • /var/www/project (venv)
    • bin/
    • include/
    • lib/
    • share/
    • myweb/
      • db.sqlite3
      • manage.py
      • myweb/
      • some_app/
        • views.py
        • ect...
      • tatic/
      • templates/

If enyone has any tips I would appreciate it!

setup info:

  • Ubuntu server 18.04
  • Apache 2.4.29
  • virtualenv w/python 3.6.7
  • Django2.2.1
melma
  • 11
  • 3
  • 3
    Don't do this. Use [`call_command()`](https://docs.djangoproject.com/en/2.2/ref/django-admin/#django.core.management.call_command). – Daniel Roseman May 22 '19 at 15:23

1 Answers1

0

You have to declare the correct relative path

some_app/
    views.py (YOU ARE HERE)
project_name/
    manage.py (YOU HAVE TO GO HERE)

So it becomes:

def home(request):
    cmd = 'python ../project_name/manage.py check'
    subprocess.Popen(shlex.split(cmd))
    return HttpResponse("<html><body>Hello World</body></html>")

Or Simply:

As Daniel Roseman wrote in his comments

call_command('check')
Davide Pizzolato
  • 679
  • 8
  • 25