To myself as a Django beginner (3)-Hello world! ---

** Tutorial list **

No. title
1 To myself as a Django beginner (1)-Project app-
2 To myself as a Django beginner (2)-What is MTV-
3 To myself as a Django beginner (3)-Hello world!-
4 To myself as a Django beginner (4)-Memo app creation-

** Last review **

Last time touched on how MTV works. From this time on, I will finally write the code!

** Let's Django-Hello World! -**

This time, I'm going to use Django to do "Hello world!", A road that cannot be avoided in programming learning. The goal this time is to understand the connection of ʻurls.py -- views.py` through "Hello World!".

** Create View **

First of all, open views.py in app1.

myapp/
    manage.py
    myapp/
        __init__.py
        asgi.py
        settings.py
        urls.py
        wsgi.py
    app1/                  
        migrations/
            __init__.py
        __init__.py
        admin.py
        apps.py
        models.py         
        tests.py

views.py <-Here it is!

I think that the contents of views.py are as follows by default.

from django.shortcuts import render

# Create your views here.

Import what you need to display "Hello World!" Here and define ** View **.

from django.shortcuts import render

from django.http import HttpResponse # Add

# Create your views here.

def hello (request): #View definition return HttpResponse ('Hello world!')

HttpResponse is read and literally, and the inside of () is returned to the screen. The preparation of ** View ** is complete. Then set the URL.

** URL settings **

The project has ʻurls.py` by default, but the app doesn't. So I will make it myself.

myapp/
    manage.py
    myapp/
        __init__.py
        asgi.py
        settings.py
        urls.py
        wsgi.py
    app1/                  
        migrations/
            __init__.py
        __init__.py
        admin.py
        apps.py
        models.py         
        tests.py
        views.py

urls.py <-add

Import views and set to associate with URL. app1/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.hello)
]

Defining a URL in this way and allocating processing according to it is called ** routing **. Then connect myapp / urls.py to ʻapp1 / urls.py. myapp/urls.py`

from django.contrib import admin

Added from django.urls import path, include # include

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

Connect to path ('', include ('app1.urls')), # app1 / urls.py ]

At this point, preparations for "Hello World!" Are complete! Now let's start the server and access http://127.0.0.1:8000/! I wonder if everyone is doing "Hello world!" hello_world.PNG

As you may have noticed, I haven't used ** Model ** and ** Template ** this time. This is because ** Model ** and ** Template ** are not always necessary when the response ("Hello World!") Is completed in views.py like this time. .. This time, I did not use ** Model ** and ** Template ** to verify "Hello World!", But when actually creating a web application, ** Model - Template - View ** is almost always needed. If you're worried about ** MTV **, go back to Last article each time.

** Summary **

--Defining a URL and allocating processing according to a request is called ** routing **. --There is no ʻurls.py in the app by default, so create it yourself and include it in your project's ʻurls.py.

Next time will create a simple memo app as a culmination of the past!

Recommended Posts

To myself as a Django beginner (3)-Hello world! ---
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
To myself as a Django beginner (2) --What is MTV?
(For myself) Django_1 (Basic / Hello World / Template)
Hello World on Django
Create a one-file hello world application with django
Hello World (beginners) on Django
From setting up a Rust environment to running Hello World
Until and after becoming [Kaggle Master]
Hello world
Create a one-file hello world application with django
Create a "Hello World" (HTTP) server with Tornado
To myself as a Django beginner (3)-Hello world! ---
Steps to create a Django project
Introduction to Ansible Part 1'Hello World !!'
From building a Python environment for inexperienced people to Hello world
How to build Hello, World on #Nix
Hello world instead of localhost in Django
Flask tutorial (from installation to hello world)
How to display Hello world in python
How to disguise a ZIP file as a PNG file
From Kivy environment construction to displaying Hello World
How to develop a cart app with Django
I'm addicted to Kintone as a data store
Django REST framework A little useful to know.
As a beginner, I searched the / proc directory
I want to upload a Django app to heroku
Pymacs hello world
[Learning memo] How to make an application with Django ~ Until Hello World is displayed ~
cython hello world
A super introduction to Django by Python beginners! Part 1 I tried to display an HTML page that only says "Hello World"
A memo to create a virtual environment (venv) before Django
I tried to create a table only with Django
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
(Python) Try to develop a web application using Django
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
How to deploy a Django application on Alibaba Cloud
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
How to build a Django (python) environment on docker
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
Steps from installing Python 3 to creating a Django app
CTF beginner tried to build a problem server (web) [Problem]
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 4-
Transit to the update screen with the Django a tag
How to run Django on IIS on a Windows server
How to reference static files in a Django project
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-
Hello World! Output list by various languages (scheduled to be updated in a timely manner)