[PYTHON] Try Django's official tutorial from scratch

What kind of person do you want to read

--People who are curious about Django but can't do the tutorial because it seems difficult --People who gave up due to an error ――The explanation is good, so anyone who wants to finish it anyway

Skip the environment settings

What you need

  1. Python3-> There is a zip file on the official website, so let's put it in
  2. If Django-> "pip" is included, start the terminal with "pip install django" and copy it immediately. If you don't have pip, put it in

How to write a description

The following points are assumed so that it is easy to understand even from scratch. --Where to write the code is written as a file path ――Specify the parts of the code that you can change by yourself ――When you say where to change the file, you may not know where to change it, so basically when you rewrite the file, the contents of the rewritten file will be written as it is, so copy and paste from all selections.

1 Create a project

django-admin startproject mysite

Put this in your terminal, django will create the required files and put them in place. You can change the "mysite" part to your liking and it's ok, but names like "test" will result in an error as the system will use them later. Make it somewhat unique.

2 Start the server

python manage.py runserver

Copy this into your terminal as well, and you'll be able to see your project in your browser.

localhost:8000

You can access it by entering it as a URL in your browser.

3 Application creation

The files you just created are called projects, and you will create apps one by one in this.

python manage.py startapp polls

This is also copied to the terminal. I will make a voting application called polls.

Make 4 views

The view is what you see when the user accesses it.

view.py


from django.shortcuts import render
from django.http import HttpResponse


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

Make the views file in the polls folder as above, copy and paste the above one as it is and delete the existing one. Then make polls / urls.py as follows.

from django.urls import path
from . import views

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

Finally mysite / urls.py

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

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

If you edit it so that it becomes k.

The rest

python manage.py rumserver

In your terminal, look at localhost: 8000 / polls in your browser, and if you see Hello, world. You're at the polls index., You're done. Continue on to the next post.

bonus

You can also write Hello, world. You're at the polls index. When creating the 4th view in html grammar, for example.

view.py


from django.shortcuts import render
from django.http import HttpResponse


def index(request):
    return HttpResponse("<h1>My First Heading</h1><p>My first paragraph.</p>")

In this case, the text will be displayed as html.

Recommended Posts

Try Django's official tutorial from scratch
Deep learning / Deep learning from scratch 2-Try moving GRU
Deep Learning from scratch
Django memo # 1 from scratch
GO Official Tutorial Note 1
Try Kaggle's Titanic tutorial
Building Linux From Scratch 10.0
RabbitMQ Tutorial (1) -From Installation to Hello World (Official Site Translation)
"Deep Learning from scratch" self-study memo (No. 15) TensorFlow beginner tutorial
Deep Learning from scratch 1-3 chapters
Django starting from scratch (part: 2)
Django starting from scratch (part: 1)
"Deep Learning from scratch" self-study memo (No. 13) Try using Google Colaboratory