[PYTHON] Django ~ Let's display it in the browser ~

Introduction

This time, I will write the procedure to display "Hello world" on the browser with Djnago. If you're new to Django, read the article below and you'll be on your way. Procedure to create application with Django with Pycharm ~ Preparation ~ Django ~ settings.py edition ~

The directories and files of the entire project are as follows.

Pycharm project
      |
      |___django project directory
      |
      |___django application directory__templates directory
      |
      |___manage.py
      |
      |___Other files

Let's go!

Edit "urls.py" of the project

First, go to "urls.py" in your django application directory. Probably something like this is written.

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

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

Please add this as below.

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app/', include('todo.urls')),
]
#Here "'app/'In many cases, enter the application name, so enter whatever you like.

By setting "include", if you enter "app /" at the end of the URL, it will jump to "urls.py" of the application. The reason there are two is that separating "urls.py" for the project and the application makes the code easier to read.

Editing the application "urls.py"

Next, write to "urls.py" of the application.

In the first place, I think there are people who say that "urls.py" is a nanja. In a word, it is "an instruction sheet that describes the material of what you want to display on the browser". Imagine a "memo of what to buy" that your mother gave you when you asked for shopping.

It is not prepared by default, so if you have not made it, let's make it.

#Move to application directory
cd application directory name

#urls.Creating a py file
touch urls.py

This is OK.

I just made it and the contents are empty, so let's write it immediately.

from django.urls import path
from .views import HomeView   #views.Loading the "HomeView" class from py.

urlpatterns = [
    path('home/', HomeView.as_view())
]
#「home/Is what you enter in the URl part.
#「HomeView.as_view()"" Is "home" in the YRL part/By typing "views".Call "HomeView" of py.

Even if it is called views.py, I think it feels like a mess, so I will explain it in the next chapter.

Editing "view.py"

views.py is a place to collect the necessary materials as instructed by urls.py. "View.py" is a child who buys ingredients according to the memo given by his mother.

Let's add it immediately. By default it should look like this:

from django.shortcuts import render

# Create your views here.

First of all. "# Create your views here." Delete this part and add as follows.

from django.shortcuts import render
from django.views.generic import TemplateView #Import of template display specialized

class HomeView(TemplateView):     #Urls a while ago."HomeView" class loaded into py
    template_name = 'home.html'   #Specifying the HTML file to use

Editing "HTML file"

Let's edit the HTML file from here. First, change to the templates directory.

cd templates

Create an HTML file in the templates directory.

touch home.html   #〇〇.Please create with html

When you create it, the contents are empty, so let's write it. First, press "!" And the Tab key at the same time. Then you will see something like the one below.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
</html>

If this does not come out, please copy it. After that, add + change as below.

<!DOCTYPE html>
<html lang="ja">    #In Japanese
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Hello World</h1># Addendum
</body>
</html>

Now you're ready to go!

Run

Let's do it!

python manage.py runserver
http://127.0.0.1:8000/」

If you click it, you will be taken to the browser. It would be perfect if you could display "Hello World" there!

At the end

This time, I introduced the procedure to display "Hello World". Let's experience small things first and grow steadily! I will continue to write articles as output, so please take a look if you like.

Recommended Posts

Django ~ Let's display it in the browser ~
Read the csv file and display it in the browser
Display Python 3 in the browser with MAMP
Display the time in Django according to the user's region (UTC)
Django-TodoList② ~ Let's display the details page ~
Python in the browser: Brython's recommendation
Switch the language displayed in Django 1.9
The meaning of ".object" in Django
Let's display the map using Basemap
Get the query string (query string) in Django
Real-time display of server-side processing progress in the browser (implementation of progress bar)
Get the client's IP address in Django
Let's claim the possibility of pyenv-virtualenv in 2021
Display pyopengl in a browser (python + eel)
Find it in the procession and edit it
Try hitting the Spotify API in Django.
If the conda environment is strange, let's put it back in mac
Display the edge
The _authenticate_with_backend function was obsolete in django auth.autenticate
Let's parse the git commit log in Python!
Models in Django
Display the script shortcut menu in the network editor
In Django, display batch (command) results sequentially in HTML
Let's automatically display the lyrics of the song being played on iTunes in Python
[Python] Display the Altair legend in the plot area
Display error message when login fails in Django
Define a division value in Django and easily reflect it on the screen
Let's display a simple template that is ideal for Django for the first time
Specify the view URL in your Django template
The story of viewing media files in Django
[Django] css in the project cannot be read
In bash, "Delete the file if it exists".
Forms in Django
Display the Django shift table! Supports 20-day closing
I want to display the progress in Python!
I referred to it when I got stuck in the django geodjango tutorial (editing)
[All-new best browser] Let's change the way the Internet works and how it works with the Brave browser.
[Django] Let's try to clarify the part of Django that was somehow through in the test
When I checked the query generated by Django, it was issued in large numbers
Display n digits after the decimal point in python
Learning notes for the migrations feature in the Django framework (2)
[Django] Display the error message specified by raise ValidationError
[Django] Perform Truncate Table (delete all data in the table)
Check if it is Unix in the scripting language
Set the form DateField to type = date in Django
Learning notes for the migrations feature in the Django framework (3)
Let's use the open data of "Mamebus" in Python
Learning notes for the migrations feature in the Django framework (1)
How to use Decorator in Django and how to make it
Check if it is Unix in the scripting language
Let's add it to the environment variable from the command ~
Python OpenCV tried to display the image in text.
[Python] Let's change the URL of the Django administrator site
Let's automatically display the lyrics of the song being played on iTunes in Python (improved version)
How to display the regional mesh of the official statistics window (eStat) in a web browser
Model changes in Django
A story that makes it easier to see Model debugging in the Django + SQLAlchemy environment
[Django] Rename the project
Let's introduce the library currently used by engineers with about 3 years of experience in Django
Enlarge the plot display.
How to write custom validations in the Django REST Framework