Django development using virtualenv Procedures from virtual environment construction to new project / new application creation and initial settings

Introduction

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

Build a virtual environment using virtualenv

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) $

Install Django in a virtual environment

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

Create a new project

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)

Create new app

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

Initial setting

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 thetouch` 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.

Get out of the virtual environment

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) $

Finally

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

Django development using virtualenv Procedures from virtual environment construction to new project / new application creation and initial settings
WEB application development using Django [Initial settings]
[Google App Engine] Flow from development environment construction to application creation
From 0 to Django development environment construction to basic operation
django project development environment construction
How to do the initial setup from Django project creation
WEB application development using django-Development environment construction-
[Basic] Unify everything from tabulation to dashboard creation! Data visualization application development with Python and Dash ~ Overview of Dash, environment construction, sample execution ~
WEB application development using Django [Admin screen creation]
Until Django application creation by terminal (development environment)
Create a Django project and application in a Python virtual environment and start the server
[Django3] Environment construction and various settings summary [Python3]
How to run a Django application on a Docker container (development and production environment)
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
From Python environment construction to virtual environment construction with anaconda
[Learning memo] How to make an application with Django ~ From virtual environment to pushing to github ~
Python development environment construction 2020 [From Python installation to poetry introduction]
[AWS] I tried using EC2, RDS, Django. Environment construction from 1
Django development environment construction memo
Development of WEB application using Django [Add data from management screen]
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Process and result of connecting to SQL Server on windows from Linux virtual environment using JDBC driver
WEB application development using Django [Django startup]
[Note] Django project creation and terminology
WEB application development using Django [Application addition]
[For beginners] Django -Development environment construction-
From Ubuntu 20.04 introduction to environment construction
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Python virtual environment construction (2017 version) pyenv and pyenv-virtualenv and virtualenv and virtualenv wrapper and pyvenv and venv
How to build an application from the cloud using the Django web framework