Django establish app The order is
startapp user
As shown in the figure , Create features app (user) success .
establish python software package (apps), Be careful Don't create directories Otherwise, the problem of package guide may occur in the later development process .python The package in is equivalent to the module , It can be found directly .
establish apps After the folder succeeds , Will function app Throw it in , After that, the developers are creating all the app In the process, you need to drop the file into apps Module package .
Throw in apps File does not mean the end of module file configuration , At this point, you must apps Directory in pycharm Marked as The root directory of the resource , If you don't mark the project, you can't find the function created by the developer app.
At this time in setting File to register the function created app(user).
If there is no warning, the creation is successful .
After the above basic operations are realized , At this point, the last thing developers need to do is to change the function app Add the path of to the middle of the system path ,
What is the system path ?
It's ours python route , That is to say, the module app and python Of sys Package to achieve unity
There are two ways to write this
The first one is (Django2 Version writing ), Use it directly os Module utilization sys Of insert Insert it :
That is
import sys
import os
sys.path.insert(0, BASE_DIR)
sys.path.insert(1, os.path.join(BASE_DIR, 'apps'))
The second is Django3 Writing , No longer use os Spliced , Instead, use path splicing ( Simple and crude ):
import sys
import os
sys.path.insert(0, str(BASE_DIR))
sys.path.insert(1, str(BASE_DIR / 'apps'))
Two contrasts :
So the above is Django The solution to how to add the module package to the system directory during project development .