Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import and run a django function at the command line

Tags:

python

django

I have a Django function "get_data_from_text_file()" that I want to test run from the command line. I tried:

>>> import v1.views
>>> get_data_from_text_file("kk")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'get_data_from_text_file' is not defined

Why is it not defined if its imported sucessfully?

like image 451
user1592380 Avatar asked Oct 09 '14 14:10

user1592380


People also ask

What command-line can be used to load data into Django?

Django loaddata is a command used for pre-populating database with data, especially during the development phase. Data is loaded from fixture files that represent serialized data. Django dumpdata is a command used for dumping data from database to fixture files. Output from dumpdata can be in various file formats.


Video Answer


1 Answers

First of all make sure you're using manage.py shell and not plain python in the command line to make this test.

You're not importing the get_data_from_text_file function, but all the view. You could either use:

v1.views.get_data_from_text_file("kk")

or try to do the import like this:

from v1.views import get_data_from_text_file

like image 110
Phob1a Avatar answered Sep 28 '22 01:09

Phob1a