J
J
Jekson2018-07-10 19:01:43
Django
Jekson, 2018-07-10 19:01:43

How to run a function in a django project from the console?

We need to execute the function. Since it is necessary to execute once to write url, view and form and display the button in the interface is superfluous. I want to execute in the console. I activate the shell in the console, go to the folder where the stata.py file is located with the following content:

from django.shortcuts import render
import json
from django.http import HttpResponse, HttpResponseRedirect
from .models import InstagramDemographicsAnalitics, Instagram, 

def get_ida_instance():    
    for key, value in data.items():
        profile = Instagram.objects.filter(userid= value['userId'])
        if profile:
            instagram = Instagram.objects.get(userid=value['userId'])
            ....
        print('Create Success')
        return HttpResponse('Create Success')
    return render(request, 'ida.html')

if __name__ == '__main__':
    get_ida_instance()

And I call stata.py
I get NameError: name 'stata' is not defined
Tell me how to call it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-07-10
@Lepilov

$ python manage.py shell

In [1]: from some_app.stata import get_ida_instance
In [2]: get_ida_instance()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question