Redirect the output of custom Django command to the browser

I have created a custom command which takes a positional and a named optional argument. It does many checks, downloads a file, unzips it and populates the database with the unzipped data.

To make the things more convenient for the users I created a simple form and created a view:

from django.views.generic.edit import FormView
from django.core import management

from .forms import DownloadForm

class DownloadFormView(FormView):
    template_name = 'frontend/download.html'
    form_class = DownloadForm
    success_url = '.'

    def form_valid(self, form):
        country = form.cleaned_data['country'][:3]
        levels = int(form.cleaned_data['country'][-1:])
        management.call_command('download_shapefile', country, levels=levels)
        return super(DownloadView, self).form_valid(form)

That works fine and now I want to provide feedback to the user, as the command might run for a while.

Is there any possibility to redirect the command output (or make it available) to the template and display it in the browser?

In my command I use:

self.stdout.write('some success message'))
self.stdout.write('some warning message'))

to output messages and don't print directly with the print method.

Asked by: Abigail141 | Posted: 30-11-2021

Answer 1

You can make your task through Ajax and just lock the screen until the server returns a response. But this is a bad practice. The best solution would be to use Celery (or something like that). This is a distributed task queue. Your data will be written to the database after execution. Until that time, temporary results will be stored in the NoSQL. In this case, this is called a broker. After that, you can use the advice given above. You just ask the server if the answer is ready. If so, you can return it and show it to the user. Thus, the load on the server will be significantly reduced.

Answered by: Sienna241 | Posted: 01-01-2022

Similar questions

bash - Redirect command to input of another in Python

I would like to replicate this in python: gvimdiff <(hg cat file.txt) file.txt (hg cat file.txt outputs the most recently committed version of file.txt) I know how to pipe the file to gvimdiff, but it won't accept another file: $ hg cat file.txt | gvimdiff file.txt - Too many edit arguments: "-" Getting to the python part... # hg...

How redirect a shell command output to a Python script input ?

This is probably something really basic, but I can not find a good solution for it. I need to write a python script that can accept input from a pipe like this: $ some-linux-command | something like this: cat email.txt | Or it will just be piped by my .forward file directly from sendmail. This means that the input file might be somet...

python - Redirect (>) command does not print some tail of output to file

I tried to print output result of my python program to log file using by this command: ./ > result.log And when i opened result.log i found one problem. It missed some tail of output i think it's about 5 - 15 of the last lines How to solve this problem? Addtionnal information: Language: Python Termianl: iTerm2 OS: O...

Using Linux redirect to file command in Python

I want to write the output of the free command of Linux to a file using Python. I have tried the following but it did not help: from subprocess import call call(["free",">","myfile"]) f = open('myfile','w') f.write(["free"])) I am new to Python so can someone guide me here to write the free command output to a file using Python?

Redirect unix command output into a file in python

import sys,re,os from subprocess import Popen, PIPE, call newCmd = 'diff -qr -b -B '+sys.argv[1]+' '+sys.argv[2]+' --exclude-from='+sys.argv[3]+' | grep pattern1\|pattrern2 > outputFile' ouT,erR = Popen(newCmd, shell=True).communicate() print ouT,erR ouT and erR are printing None, None and the outputFile is a blank file. When i execute the same 'newCmd' in normal shell, ...

python - Redirect command line results to a texture (html) in real time

I would run a server and view real-time results in a textarea on a html page. Is it possible? For now I get to do it when the command is finished, but I would like to do while the server is running. I tried: r = subprocess.Popen(argServer, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) stdout, stderr = r.communicate() print """<textarea>"""...

python - Run shell command pipe with output redirect lively ?

I am trying to run several lines of shell commands by Python, these shell commands are like: sudo apt-get install xxx | sudo apt-get update cd ~/somefolder make && sudo make install I want to run these lines of commands line by line, like: for line in commands: Run(line) # line could be any valid shell commands inside function Run, I would lik...

python - Redirect command prompt to tkinter

I'm trying to redirect the powershell-output into a text box in Tkinter. I specifically want the print statements from the command prompt. does anyone know how I can go about this? I followed this but it won't print to the text box. my final function looks like this: def start_script(): ...

python - Not able to redirect output of command to output file

I am writing a python script to take dump backup of Mongo. I want my output to be redirect to a text file that can be used for further reference. I have tried sys.stdout but it is only printing the output of print command sys.stdout !/usr/bin/python3 import os import time import datetime import sys import subprocess import glob ''' mongo backup by python ''' BKP_DIR = "/app...

cmd - Using '>' in the command prompt will redirect output for one python script, but not for another

I currently have a python script called added to the Windows PATH variable so that I can run it from any directory. consists of the following files and functions: cat tac cut paste

bash - Redirect command to input of another in Python

I would like to replicate this in python: gvimdiff <(hg cat file.txt) file.txt (hg cat file.txt outputs the most recently committed version of file.txt) I know how to pipe the file to gvimdiff, but it won't accept another file: $ hg cat file.txt | gvimdiff file.txt - Too many edit arguments: "-" Getting to the python part... # hg...

Python + Django page redirect

How do I accomplish a simple redirect (e.g. cflocation in ColdFusion, or header(location:http://) for PHP) in Django?

python - How to pass information using an HTTP redirect (in Django)

I have a view that accepts a form submission and updates a model. After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page. How can I "pass" this message to the other page? HttpResponseRedirect only accepts a URL. I've seen this done bef...

python - Django: Redirect to previous page after login

I'm trying to build a simple website with login functionality very similar to the one here on SO. The user should be able to browse the site as an anonymous user and there will be a login link on every page. When clicking on the login link the user will be taken to the login form. After a successful login the user should be taken back to the page from where he clicked the login link in the first place. I'm guessing that I ...

How to redirect the output of .exe to a file in python?

In a script , I want to run a .exe with some command line parameters as "-a",and then redirect the standard output of the program to a file? How can I implement that?

python - How to Redirect To Same Page on Failed Login

The Django framework easily handles redirecting when a user fails to log in properly. However, this redirection goes to a separate login page. I can set the template to be the same as the page I logged in on, but none of my other objects exist in the new page. For example, I have a front page that shows a bunch of news articles. On the sidebar is a login form. When the user logs in, but fails to authenticate, I wou...

cgi - How to redirect to another page in Python in CGIAr

If I want to redirect the user from a cgi script to a HTML page using core libraries from the following: import cgi Please could someone point me in the right direction. Nothing more. Simply redirect from cgi script to html page. How ever you would do this Python. If you have to physically write out the HTTP Response Headers to achieve this, then I would appreciate any i...

How to redirect stderr in Python? Via Python C API?

This is a combination of my two recent questions: [1] Python instance method in C [2] How to redirect stderr in Python? I would like to log the output of both stdout and stderr from a python script. The thing I want to ask is...

How to redirect stderr in Python?

I would like to log all the output of a Python script. I tried: import sys log = [] class writer(object): def write(self, data): log.append(data) sys.stdout = writer() sys.stderr = writer() Now, if I "print 'something' " it gets logged. But if I make for instance some syntax error, say "print 'something# ", it wont get logged - it will go into the console instead. Ho...

linux - Redirect stderr to stdout on exec-ed process from python?

In a bash script, I can write: exec 2>&1 exec someprog And the stderr output of someprog would be redirected to stdout. Is there any way to do a similar thing using python's os.exec* functions? This doesn't have to be portable, just work on Linux.

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python