[PYTHON] [Can be done in 10 minutes] Create a local website quickly with Django

prologue

Let's create a local website quickly with Django. Article environment  Window 10  Django==3.1  Python 3.7.6

procedure

It is a prerequisite that python is installed. For now, install Django with pip install.

pip install django

Once Django is installed, we'll build it.

Execute the command in the folder where you create the project. Hello World is the project name.

django-admin startproject HelloWorld

The composition looks like this.
└─HelloWorld
    │  manage.py
    │
    └─HelloWorld
            asgi.py
            settings.py
            urls.py
            wsgi.py
            __init__.py

Next, create an app. Goodbye is the app name.
python manage.py startapp  Goodbye

The composition looks like this.
│  manage.py
│
├─Goodbye
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations
│          __init__.py
│
└─HelloWorld
    │  asgi.py
    │  settings.py
    │  urls.py
    │  wsgi.py
    │  __init__.py
    │
    └─__pycache__
            settings.cpython-37.pyc
            __init__.cpython-37.pyc

Make your project aware of your app. Update settings.py that manages the project. Added the app name under "INSTALLED_APPS".
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Goodbye',
]

Set the URL for site access. Updated urls.py in the project folder. The url is specified as the first argument of path. The second argument uses include so that you can refer to urls.py on the application side.
from django.contrib import admin
from django.urls import path,include

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

Also update urls.py on the application side. The first argument of path is already specified in urls.py on the project side, so leave it empty. The second argument specifies the view to be called when the url is accessed. The third argument is any name.
from django.urls import path
from . import views

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

Now you are ready to access. Next, easily create the content to be displayed on the access screen. Pass the request to the separate html using render.
from django.shortcuts import render

# Create your views here.

def get_data(request):
    if "data" in request.GET:
        param = {'test':request.GET.get("data")}
    return render(request, 'Goodbye/index.html', param)

Create an html to pass the data. I created a templates folder in the app folder, a folder with the app name under it, and created html under it. Receive data with {{variable}} and display it.
<html>
<body>
  <p>{{test}}</p>
</body>
</html>

All you have to do is start and access the development server that comes standard with Django. The passed variable is specified by a parameter. ![1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/538462/b5191946-0a13-6c05-cf6d-a54175fa9ecd.png)

epilogue

It took me a while to write the article, but it took about 10 minutes to display it. It's easy.

Recommended Posts

[Can be done in 10 minutes] Create a local website quickly with Django
Build a Django environment with Vagrant in 5 minutes
A story that heroku that can be done in 5 minutes actually took 3 days
Create a homepage with django
Create a flag in settings that will be True only when testing with Django
Text analysis that can be done in 5 minutes [Word Cloud]
Create a web API that can deliver images with Django
Create a file uploader with Django
Create a LINE Bot in Django
Make a Spinbox that can be displayed in HEX with Tkinter
Create a custom field where enum can be specified in choices
Create a web app that can be easily visualized with Plotly Dash
I wanted to quickly create a mail server that can be used freely with postfix + dovecot on EC2
You can do it in 5 minutes !? Create a face detection API with FastAPI and OpenCV and publish it on Heroku
I want to create a priority queue that can be updated in Python (2.7)
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
You can easily create a GUI with Python
Create a dashboard for Network devices with Django!
I made a familiar function that can be used in statistics with Python
Create a new page in confluence with Python
Create a one-file hello world application with django
Configure a module with multiple files in Django
How to create a Rest Api in Django
Until you create a new app in Django
Quickly build a Python Django environment with IntelliJ
[Docker] Create a jupyterLab (python) environment in 3 minutes!
I tried to easily create a high-precision 3D image with one photo [1]. (Depth can now be edited in PNG.)
Morphological analysis and tfidf (with test code) that can be done in about 1 minute
A mechanism to call a Ruby method from Python that can be done in 200 lines
Create a Todo app with Django REST Framework + Angular
I tried to create a table only with Django
Japanese can be used with Python in Docker environment
Create a Todo app with the Django REST framework
ANTs image registration that can be used in 5 minutes
Create a Todo app with Django ③ Create a task list page
Dynamically create tables in schema with Django, dynamically generate models
Create a fake Minecraft server in Python with Quarry
Create a Todo app with Django ⑤ Create a task editing function
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Create a Django schedule
It seems that Skeleton Tracking can be done with RealSense
[Django] Manage settings like writing in settings.py with a model
Create a list in Python with all followers on twitter
NumPy zeros can be defined even with a size of 0
Recommendations for django, wagtail ~ Why develop a website with python ~
Create a child account for connect with Stripe in Python
Let's create a script that registers with Ideone.com in Python.
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Until I return something with a line bot in Django!
Create an authentication feature with django-allauth and CustomUser in Django
Create a Todo app with Django ① Build an environment with Docker
I investigated the pretreatment that can be done with PyCaret
Let's make a diagram that can be clicked with IPython
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
[For beginners] Baseball statistics and PyData that can be remembered in 33 minutes and 4 seconds ~ With Dai-Kang Yang
Create a function in Python
Minimal website environment with django
Create an API with Django
Create a Django login screen