It is a web framework for developing web services. An open source web framework written in Python. It is one of the most famous frameworks used by programmers around the world.
Used in service development such as
I'd like to share the introductory content of Django, a web service framework, for notes. I think it's easier to get started if you have some knowledge of Python.
The development environment is macOS (Catalina), Python3.7, Django3. We recommend developing in a virtual environment. Since it is expected that Django 4 will be provided in the future, if you develop on the same PC, it is important to keep the development environment independent for each project in order to proceed with multiple projects.
If you are using macOS, you should use either of these to prepare the virtual environment. ** Please note that coexistence of pyenv and pipenv may not work. ** **
I will briefly explain how to build a virtual environment using pipenv and pyenv-virtulenv.
It is assumed that Homebrew is already installed and pip3 can be used.
$ pip3 install pipenv
Or
$ brew install pipenv
Next, install Django. This time, create a development directory (django) on Desktop.
#Move current directory to Desktop
$ cd ~/Desktop
#Create django directory
$ mkdir django
#Move current directory to django
$ cd django
After moving to the created django directory, install Django using pipenv.
$ pipenv install django==3.0
After running the above command, two new files will be created in your django directory
Next, execute the command to create the virtual environment.
$ pipenv shell
You can now activate it. If you're in a virtual environment, you'll see something like (django) on the left.
#If you are in a virtual environment properly
(django)$
Please refer to @hedgehoCrow for how to install pyenv-virtualenv.
How to install pyenv-virtualenv
After installing pyenv-virtualenv, we will actually build a virtual environment. First, check the version of python etc. that can be used now. (One of the pythons must be installed)
$ pyenv versions
[output]
system
2.7.16
* 3.7.4
anaconda3-5.3.1
You should see an output like this. Those marked with * are the current environment. Create the environment you want to build here. Execute the following command. Let's create a virtual environment called django for python 3.7.4.
$ pyenv virtualenv 3.7.4 django
Then, it will be added as follows.
$ pyenv versions
[output]
system
2.7.16
* 3.7.4
3.7.4/envs/django #Added environment
anaconda3-5.3.1
Then, in order to work in the virtual environment you just created, move to the directory you want to work in and execute the following command.
$ pyenv local django
This completes the construction of the virtual environment. All you have to do is install Django in this environment.
$ pip3 install django
Let's create a new Django project and start the server.
Move the following command to Desktop / django and execute it to create a project. ** (Running in virtual environment with pyenv-virtualenv) **
$ django-admin startproject test_project .
Don't forget the last period (.)! Without it, the file structure in the django directory will be incorrect. The file structure in the django directory is as follows.
├── django
├── manage.py
├── test_project
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
By adding an app to the created project, you can create a service with various functions.
Execute the following command in Desktop / django to start the server.
$ python manage.py runserver
[output]
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 20, 2020 - 07:26:31
Django version 3.0.4, using settings 'helloworld_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
A warning message will appear in red letters, but you don't have to worry about it now. The right side of the second line from the bottom is the server address. Try searching with your browser. If a display like this appears, the server has started successfully for the time being. You can use Control-C to stop the server.
Next time, I will display "Hello World". Introduction to Django 2
Django for Beginners 3.0
Recommended Posts