I may forget the procedure from a clean state to virtual environment construction-new project / new application creation / initial setting, so I would like to keep it.
** It is a prerequisite that Python3 series and virtualenv, which is a virtual environment construction tool, are installed. ** **
The author's environment is as follows.
** Check in advance which version of Python is supported by each version of Django. ** ** https://docs.djangoproject.com/ja/2.2/faq/install/#faq-python-version-support
First, create a folder in any location (desktop) to put the project to be created with the mkdir
command. (The name is sampleproject as an example this time)
Enter the folder created by the cd
command.
$ mkdir sampleproject
$ cd sampleproject
Execute the following command to build a virtual environment.
The name can be arbitrary, but it is better to use project name-env
for easy understanding.
$ virtualenv sampleproject-env
Execute the following command to enter the built virtual environment.
Success if (project name-env)
is displayed before the $ mark on the terminal.
$ . sampleproject-env/bin/activate
#Success if the following is displayed
(sampleproject-env) $
Continue to install Django in your virtual environment. This time I will install Django 2.2 as an example. If the version is displayed as 2.2 with the second command, the installation is successful.
(sampleproject-env) $ pip install django==2.2
(sampleproject-env) $ python -m django --version
When creating a new project, you can use the $ django-admin startproject [arbitrary project name] .
command. (This time, the name is sampleproject)
(sampleproject-env) $ djando-admin startproject sampleproject .
There are the following differences when. Is added to the end of the above command and when it is not added.
If you add., You can save the trouble of creating one extra folder. ** **
sampleproject(First created folder, command execution location)/
└ sampleproject(Project created by the above command)
If you do not add., Another folder will be created from the location where you executed the ** command. ** ** An image in which the folder structure increases by one level. It is used when creating a project directly without creating a new folder with mkdir introduced at the beginning. It is not recommended in actual development because the folder structure is complicated and difficult to understand.
sampleproject(First created folder, command execution location)/
└ sampleproject/
└ sampleproject(Project created by the above command)
When creating a new application, execute the following command from the folder where manage.py
exists. (The name is sample this time)
(sampleproject-env) $ python3 manage.py startapp sample
Edit the contents of TEMPLATES in settings.py in sampleproject.
Specify the location where the HTML file is stored. (HTML files are stored in the templates folder inside BASE_DIR)
After setting, create a templates folder in the folder containing manage.py
.
sampleproject/settings.py
"""abridgement"""
TEMPLATES = [
{
"""abridgement"""
'DIRS': [BASE_DIR, 'templates'],
"""abridgement"""
],
},
},
]
"""abridgement"""
Set the created app name. (The application name is the sample created as an example this time)
sampleproject/settings.py
"""abridgement"""
INSTALLED_APPS = [
"""abridgement"""
'sample',
]
"""abridgement"""
Move to the folder of the application created by the cd
command. (Sample folder as an example this time)
Create a new ʻurls.pywith the
touch` command.
(sampleproject-env) $ touch urls.py
Set the connection of the created application to urls.py in the project folder. (Sample folder as an example this time)
sampleproject/urls.py
from django.contrib import admin
#Add include method
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
#The behavior when no URL is described is the root page(home screen)Becomes
#That is, call the sample app
path('', include('sample.urls')),
]
This completes the initial settings.
Execute the following command to exit the virtual environment built with virtualenv.
(sampleproject-env) $ deactivate
To re-enter the virtual environment, do the following: (This time, sample project is used as an example)
$ . sampleproject-env/bin/activate
#Success if the following is displayed
(sampleproject-env) $
I didn't use Anaconda because of the impact of dependencies and the potential for other issues when setting up a Django development environment with Anaconda. This time, I uninstalled Anaconda and then built the environment. I had a hard time creating a new project in the Django environment.
Recommended Posts