pythondjango

django-background-task not functioning


my django-background-task does nothing, even if I just ask it to do something simple like printing a string. I dont know what I am doing wrong though.

views.py:

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
from .forms import UserRegistrationForm
from .models import Player, PlayerStats, TotalLevels
from django.core import management
from background_task import background
import random

# background task
@background(schedule=5)
def play_run():
    print('it works!')
    # management.call_command('runscript', 'runnerscript')

def random_logo():
    logo = random.randint(1, 5)
    logo_file = 'logo-nav'+str(logo)+'.png'
    return logo_file

# Main website views

def home(request):
    return render(request, template_name='main/home.html')

# bloxors website specific views

def homepage(request):
    logo_file = random_logo()
    return render(request, template_name='bloxors/homepage.html', context={'logo':logo_file})

# view in which background task is called
@login_required
def play_redirect(request):
    play_run()
    return HttpResponse('Please wait while we redirect you...')

Can someone help me by telling what is going wrong?

As always, I appreciate any and all answers!


Solution

  • While the server is running, open another terminal or command prompt with your virtual environment activated then run python manage.py process_tasks

    https://django-background-tasks.readthedocs.io/en/latest/