I'm making an app with Django
--Run Python script to crawling and fetch data --Formatting in a python script --I want to save it in django's DB and use it.
This is my goal, but here I will access Django from Python and summarize it as a DB and do what I like.
As I mentioned at the beginning, what I want to do is import the django app from the python interactive shell and play with it from there.
python_django.py
import os,sys,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "you_apps_name.settings") # *1
sys.path.append(r'/Users/kanuchi34/Projects/python/dj/quotes') # *2
django.setup() # *3
Annotate in order
I haven't figured out what this is doing in detail yet, but in short it seems to provide some basic information for using DJANGO.
Actually, there seem to be several ways to execute python code, and one of them
manage.Call py with code execution
There was a method like this, and when I wondered, "I'm sure this guy is taking over various settings," I found this * 1 code.
So I think django is django, calling DJANGO_SETTING_MODULE, which would be in python, and then making python and django available.
This code isn't bad to wear, but you can omit it. It seems necessary to pass the python path to the environment under the app directory.
It doesn't matter if you don't run the python script in the app directory
django.setup()
I forgot to call this all the time and went around for about an hour. I think it's possible to setuup the environment of django
If you don't call this, you'll get an error statement like AppDirectryIsntReady and you won't be able to proceed.
Everyone wrote the ** your_apps_name ** in the annotation * 1 that I wrote in this article, and I didn't understand what this meant at all (I wonder if there are beginners).
1. config.local
2. myproj.settings
3. myapp.settings
4. local
5. config
I finally found out by looking at manage.py, but I wonder if I can't unify this area. ..
Recommended Posts