[PYTHON] Start today with Django

We'll show you how to build a Django environment and create a simple Hello Word app.

Environment

Execute the following command.

$ pip3 install Django

After the installation is completed normally, you can check the version with the following command.

$ python3
>>> import django
>>> print(django.get_version())

This completes the environment construction, isn't it? Now let's make a PJ.

Project creation

Move to the directory where you want to create the project and execute the following command. Just run it and you should have a directory called mysite and a set of files.

$ django-admin startproject mysite

What is django-admin?

A utility for performing Django administration tasks. Most tutorials don't use django-admin after project generation, they use manage.py, a thin wrapper around django-admin.

There are two things that manage.py does:

  1. Add the project package to sys.path.
  2. Set the DJANGO_SETTINGS_MODULE environment variable to point to your project's settings.py.

When it's time to touch it for the first time, I think it often doesn't come to the fore. Is manege.py slower because of the extra processing? That's not the case, so I think it's best to follow the tutorial instructions that you refer to when you first touch it.

Now let's take a look at the generated files.

What Django automatically generates

If the project generation command is successful, the project will be completed with the following file structure. スクリーンショット 2019-11-10 11.17.33.png

Execute the following command in the generated project directory to start the local server.

$ python3 manage.py runserver

After executing the above command, access the following to display the screen. Easy. http://127.0.0.1:8000/

If port 8000 (default) is used by another service, specify the port as shown below and execute.

$ python3 manage.py runserver 8080

After confirming the default TOP page, let's finally display the screen you made.

App generation with Django

Execute the following command on mysite. The app should have been generated with the name firstapp.

$ python3 manage.py startapp firstapp
スクリーンショット 2019-11-10 11.47.23.png

Under firstapp, there is an easy-to-understand view definition file called views.py. Please make the contents as follows.

views.py


from django.http import HttpResponse

def index(request):
    return HttpResponse("The first application I made with Django.")

After defining the view, it's time to set up the routing. Create a file called urls.py under the firstapp app and make the contents as follows.

firstapp/urls.py


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

After setting the routing of the application, add it to the routing of the route project. Modify urls.py under mysite as follows.

mysite/urls.py


from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('firstapp/', include('firstapp.urls')),
    path('admin/', admin.site.urls),
]

Execution confirmation

After coding, start the server again and try accessing the following. http://localhost:8000/firstapp/

$ python3 manage.py runserver

I think the characters are displayed on the screen. In making a simple screen, you can do it just by understanding the following roles.

  1. Set up routing urls.py
  2. Make a screen views.py
  3. manage.py, a utility for performing Django administration tasks

From the next time onward, I will introduce what happens if you create a DB to make it more like an application and perform the same procedure in Visual Studio.

Recommended Posts

Start today with Django
Internationalization with django
Start Django Tutorial 1
CRUD with Django
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output PDF with Django
Markdown output with Django
Start M5Stack with UIFlow
Use Gentelella with django
Start Django in a virtual environment with Pipenv
Getting Started with Django 1
Send email with Django
File upload with django
Django --start project without start project
Start IPython with virtualenv
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start a Django project
Getting Started with Django 2
Things to do when you start developing with Django
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Do AES encryption with DJango
Getting Started with Python Django (6)
Real-time web with Django Channels
Double submit suppression with Django
Django REST framework with Vue.js
Getting Started with Python Django (5)
Login with django rest framework
Qiita API Oauth with Django
Until the start of the django tutorial with pycharm on Windows
Test Driven Development with Django Part 3
Steps to develop Django with VSCode
Test Driven Development with Django Part 4
Set up social login with Django
Test Driven Development with Django Part 6
Measure Django application coverage with Coverage.py
Handle csv files with Django (django-import-export)
HTTPS with Django and Let's Encrypt
Deploy a Django application with Docker
Common html to rent with Django
Django Model with left outer join
Django
[Poetry] Start Django's runserver with Poetry script