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(self.style.SUCCESS('some success message'))
self.stdout.write(self.style.WARNING('some warning message'))
to output messages and don't print directly with the print
method.