A story about someone who wanted to import django from a python interactive shell and save things to a DB

background

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.

Version etc.

Go to the main story

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

Note * 1

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.

Note * 2

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

Note * 3

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.

I was addicted to

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

A story about someone who wanted to import django from a python interactive shell and save things to a DB
About Python, from and import, as
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
A story about Python pop and append
Import Excel file from Python (register to DB)
A story about modifying Python and adding functions
Steps from installing Python 3 to creating a Django app
A story about a Python beginner who was about to be crushed by ModuleNotFoundError: No module named'tweepy'
How to run a Python program from within a shell script
A story about trying to implement a private variable in Python.
A story about a beginner participating in a project by Django from team building to product release in 6 weeks
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
A story about how to study English by a stupid student in grade Biri who raised his TOEIC score from 300 to 840 in a year and a half.
A story about adding a REST API to a daemon made with Python
A story about trying to run multiple python versions (Mac edition)
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
Django: Import a class from a string
A story about trying to run JavaScripthon on Windows and giving up.
A story about custom users having a sweet and painful look at Django
A story about a beginner making a VTuber notification bot from scratch in Python
A story I was addicted to when inserting from Python to a PostgreSQL table
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
A story about trying to connect to MySQL using Heroku and giving up
Connect to postgreSQL from Python and use stored procedures in a loop.
[python] Send the image captured from the webcam to the server and save it
[python] A story about collecting twitter account names from handle names (like @ 123456) by combining BeautifulSoup and Excel input / output.
A story about converting HTML to PDF with WeasyPrint + matplotlib and embedding graphs [Beginners learn python with a reference book]