[PYTHON] Django tutorial summary for beginners by beginners ① (project creation ~)

Introduction

Now popular Python ... Python for AI development and deep learning, Python for image processing, Python for log analysis, Python with Django for creating web applications ...

I felt like I heard a voice like this "Everyone is doing Python, only you who aren't doing it"

I don't know, I'll do Python.

Learn from the official Django documentation. https://docs.djangoproject.com/ja/3.0/

The official documentation is in Japanese and is (probably) easy to read as it is.

Target reader level

I'm currently B2. I'm not an information department.

I usually develop web applications on Elixir / Phoenix. (Phoenix is Elixir's web framework)

I've touched Python lightly in class.

So as a level, I've used other web frameworks, but I've never touched Python as well as Django. I think it will be an article for about a few people.

Also, for some reason, the installation was done on my PC (really why), so I will skip the introduction here.

By the way, if the version comes back with the following command, you can even install Django.

$ python -m django --version

table of contents

This article is the first article in the series.

Summary of Django tutorials for beginners by beginners ① (project creation ~) Django tutorial summary for beginners by beginners ② (Model, Admin) Django tutorial summary for beginners by beginners ③ (View) Django tutorial summary for beginners by beginners ④ (Generic View) Django tutorial summary for beginners by beginners ⑤ (test) Django tutorial summary for beginners by beginners ⑥ (static file) Summary of Django tutorials for beginners by beginners ⑦ (Customize Admin)

Creating your first Django app, part 1

https://docs.djangoproject.com/ja/3.0/intro/tutorial01/

Project creation

$ django-admin startproject mysite

This will create the following

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

Go from above

mysite/ -The mysite directory, which is the root directory, has no particular meaning, and you can rename it appropriately at any time.

manage.py -Command line utility. Perform project-related operations such as starting a server.

mysite ·app name

__init__.py An empty file that tells Python that it is a Python package

settings.py -Project configuration file

urls.py ・ Write a URL (a router called Rails?)

asgi.py wsgi.py Well I do not know

Server startup

$ python manage.py runserver

Go to http://127.0.0.1:8000/ スクリーンショット 2020-01-01 23.41.17.png If this comes out, it is a success.

Also,

$ python manage.py runserver 8080

Change the port and start

$ python manage.py runserver 0:8000

You can also specify the server ip with.

Create a Polls application

Create it with the following command. Also, it seems that you can create an application anywhere in the file.

$ python manage.py startapp polls

The contents of the created application are as follows.

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Create your first view

Edit polls / views.py.

polls/view.py


from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

It is necessary to associate the url. Create urls.py.

polls/urls.py


from django.urls import path

from . import views

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

After that, it will be reflected in mysite / url.py.

mysite/url.py


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

urlpatterns = [
    path('polls/', include('polls.urls')),  #add
    path('admin/', admin.site.urls),
]

This series of flow is a little different from routing such as Rails.

If you access http: // localhost: 8000 / polls / with the settings so far スクリーンショット 2020-01-02 1.42.43.png

If this happens, you are successful!

(Extra edition) About path

path takes an argument as follows

path(route, view, kwargs=None, name=None)

Path pattern such as route" polls / "

view view called when the pattern matches

kwargs Pass any keyword argument as a dictionary to the target view.

name Give the URL a name so that it can be called from anywhere in the project.

Recommended Posts

Django tutorial summary for beginners by beginners ① (project creation ~)
Django tutorial summary for beginners by beginners ③ (View)
Django tutorial summary for beginners by beginners ⑤ (test)
Django tutorial summary for beginners by beginners ⑥ (static file)
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
Django tutorial summary for beginners by beginners ④ (Generic View)
Python Django tutorial summary
Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
Django Getting Started: 2_ Project Creation
Reference resource summary (for beginners)
[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
[Note] Django project creation and terminology
Machine learning summary by Python beginners
[For beginners] Django -Development environment construction-
What is scraping? [Summary for beginners]
TensorFlow Tutorial MNIST For ML Beginners
Django Girls Tutorial Summary First Half
Pandas basics summary link for beginners
[Deprecated] Chainer v1.24.0 Tutorial for beginners
TensorFlow Tutorial -MNIST For ML Beginners
[Explanation for beginners] TensorFlow tutorial Deep MNIST
Django Summary
[Linux command summary] Command list [Must-see for beginners]
Commands for creating a new django project
Django Summary
Linux operation for beginners Basic command summary
Django Tutorial (Blog App Creation) ⑤ --Article Creation Function
WebApi creation with Python (CRUD creation) For beginners
Django Tutorial (Blog App Creation) ④ --Unit Test
A textbook for beginners made by Python beginners
Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
Django Tutorial (Blog App Creation) ① --Preparation, Top Page Creation
Pandas basics for beginners ③ Histogram creation with matplotlib
An introduction to object-oriented programming for beginners by beginners
Until Django application creation by terminal (development environment)
About Nim higher-order functions for Nim beginners written by Nim beginners
Django Tutorial (Blog App Creation) ③ --Article List Display
Conducting the TensorFlow MNIST For ML Beginners Tutorial
Django Tutorial (Blog App Creation) ⑦ --Front End Complete
Python Django Tutorial (5)
django table creation
Django 1.9 for internationalization
django tutorial memo
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Django Project Baseline
Django filter summary
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
[Translation] NumPy Official Tutorial "NumPy: the absolute basics for beginners"
I tried the MNIST tutorial for beginners of tensorflow.
Django tutorial (blog application creation) ② --model creation, management site preparation
Summary of pre-processing practices for Python beginners (Pandas dataframe)
MNIST image generation program creation by DCGAN (tensorflow tutorial)
[Linux] Basics of authority setting by chmod for beginners
[For beginners] Django Frequently used commands and reference collection