Django's first Hello World

I'm a fledgling engineer for the second month since I started studying Django. Since this is my first post, I will record the process of outputting "Hello World" to the browser with Django.

environment

--Developed on Mac

Install Django

pip install django==2.2.6

First, install Django with the above command. This time we're using Django 2.2.6, so add == 2.2.6 after django to specify the version.

Just in case, please check if it is actually installed with the following command.

$ python -m django --version
2.2.6

Create a project

After installation, the next step is to create a project. First, create a directory called myapp with the following command.

$ mkdir myapp 

afterwards,

$ cd myapp 

The above command will take you to the myapp directory and run the command below to create your project.

myapp


$ django-admin startproject conf . 

Check if a project called conf has been created with the following command.

myapp


$ ls 
conf  manage.py 

It's okay if conf and mange.py have been created. The entire directory structure diagram should look like this:

myapp
|
├── manage.py
|
└── conf
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Create application

Django allows you to create multiple applications within your project. This time, we will create an application called hello.

myapp


$ python manage.py startapp hello 

It should have the following directory structure diagram.

myapp
|
├── manage.py
|
├── conf
|   ├── __init__.py
|   ├── settings.py
|   ├── urls.py
|   └── wsgi.py
|
└── hello
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

At this point, the hello application has not been applied, so you need to configure the conf's settings.py to tell you that the hello application has been added.

conf/settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]

Add 'hello' to ʻINSTALLED_APPS` and it will be recognized and applied. Every time you create a new application, you need to add it as described above.

URL settings

Next, set the URL. You can add the url pattern only to ʻurl.pyinconf`, but it is a best practice to set it separately for each application because it becomes difficult to manage as the setting becomes larger.

Edit ʻurl.py in conf` as follows.

conf/urls.py


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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('hello.urls.py')),⇦ Add
]

First, import ʻinclude and add path ('', include ('hello.urls.py')), in ʻurl patterns. It uses the include function to load the hello application ʻurls.py. This way you can just focus on editing the hello application' ʻurls.py.

Next, create a file called ʻurls.py in your hello application. ʻUrls.py on the application side is not created by startapp, so you need to create it arbitrarily.

After creating ʻurls.py in the hello` application, edit it as follows.

hello/urls.py


from django.urls import path

from . import views

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

from .import view is importing views.py from the same hierarchy. For path ('', views.index),, specify the url path in the first argument (this time it is empty), and in the second argument, specify the view function that matches the url pattern (. We will create the view function in the next step). In the third argument, write as name ='specify name' for reverse lookup of url. but , I don't need it this time, so I'm omitting it. Not filled in.

Creating a View function

Well, it's finally the last work. Edit view.py as follows.

hello/views.py


from django.http import HttpResponse


def index(request):
    return HttpResponse('Hello World')

First, import HttpResponse. Next, I created path ('', views.index), in ʻurls.py of the helloapplication earlier, but since the index in the second argument is the function used this time, It is def index (requset) . HttpResponse ('Hello World')` puts the value you want to return as a response in () and returns it as an HttpResponse object.

Now run python manage.py runserver. It is okay if the following display appears.

February 01, 2020 - 05:59:05
Django version 2.2.6, using settings 'conf.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open http://127.0.0.1:8000 in your browser.

hello world.png

If it is displayed like this, it is successful. Congratulations! !! !! !!

A word

If you can display Hello World, it's almost like taking a big step toward becoming an engineer. It may seem difficult, but if you continue to learn, you will find it fun. I'm still in the beginning in the second month, but I'd like to keep sending out.

Recommended Posts

Django's first Hello World
Hello world
Pymacs hello world
cython hello world
web2py memo: Hello World
hello world with ctypes
RabbitMQ Tutorial 1 ("Hello World!")
Hello, World with Docker
Hello World on Django
First python ① Environment construction with pythonbrew & Hello World !!
Hello world with flask
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Until hello world with zappa
Programming language in "Hello World"
Hello World in GO language
Hello World (beginners) on Django
Python starting with Hello world!
Let's do "Hello World" in 40 languages! !!
Introduction to TensorFlow --Hello World Edition
Hello world! (Minimum Viable Block Chain)
[Note] Hello world output with python
cout << "Hello, World! \ N" in python
Hello World in Flask [Appropriate memo]
Code: 2 "Hello World" in "Choregraphe-Python script"
Hello World! By QPython with Braincrash
Python #Hello World for super beginners
Introduction to Ansible Part 1'Hello World !!'
[Python] Web application from 0! Hands-on (2) -Hello World-
Hello World and face detection with opencv-python 4.2
Hello World with Raspberry Pi + Minecraft Pi Edition
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
Hello World! By QPython with Brainfu * k