I added some python file in my django project but that python file did not execute. How can i run that python scrip in django.
-
What do you mean by "did not execute"? Also you mean using it e.g. in a Django view, or do you mean run a script independently while having access to the Django environment like the database?bakkal– bakkal2015-06-24 17:50:29 +00:00Commented Jun 24, 2015 at 17:50
-
@bakkal i want to run indivisually on background...i want to process some data on backgroung and update.....but in django project.....means when i start django server i want that script to start.......Naresh– Naresh2015-06-24 17:53:43 +00:00Commented Jun 24, 2015 at 17:53
1 Answer
OK it seems you want to run a script outside of the HTTP request/response cycle, I'd recommend you make a Django admin command, because a script will need to e.g. have access to the database environment to update records
from django.core.management.base import BaseCommand
from yourapp.models import Thing
import your_script
class Command(BaseCommand):
help = 'Closes the specified poll for voting'
def handle(self, *args, **options):
# Do your processing here
your_script.process(Thing.objects.all())
With this you can call ./manage.py process_thing and it'll run independently
More on Django admin commands here, https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/
If the processing is triggered programmatically e.g. from user requests, you'll have to setup a queue and have a task job created for each request, I'd try Celery, more on that here http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html