[PYTHON] Getting Started with Django 1

What is Django

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.

Initial setting

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. ** **

Virtual environment construction

I will briefly explain how to build a virtual environment using pipenv and pyenv-virtulenv.

Virtual environment construction with pipenv

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

Building a virtual environment with pyenv-virtualenv

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

Start the server

Let's create a new Django project and start the server.

Create a new project

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.

Server startup

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. Screen Shot 2020-03-20 at 16.31.28.png 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

Reference material

Django for Beginners 3.0

Recommended Posts

Getting Started with Django 1
Getting Started with Django 2
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Getting Started with Python Django (5)
Django 1.11 started with Python3.6
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
Getting started with apache2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Get started with Django! ~ Tutorial ⑤ ~
Getting Started with Go Assembly
Getting Started with PKI with Golang ―― 4
Django Getting Started: 2_ Project Creation
Django Getting Started: 4_MySQL Integration
Django Getting Started: 3_Apache integration
Get started with Django! ~ Tutorial ④ ~
Get started with Django! ~ Tutorial ⑥ ~
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Python3 | Getting Started with numpy
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
How to get started with Django
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with CPU Steal Time
Internationalization with django
CRUD with Django
Grails getting started
Step notes to get started with django
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Flask with Azure Web Apps
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Getting Started with Lisp for Pythonista: Supplement