[PYTHON] Try using Django's template feature

Overview

This article walks you through the steps of a beginner developing a coupon distribution service for the iPhone with a RESTful API and swift. It is a very detour implementation because it was implemented while examining the technical elements one by one.

Django's template feature isn't really needed, but I'll give it a try for learning. Based on the code created in the previous Let's make a simple web API with (for beginners) Django.

reference

Python Django Super Introductory by Yano Palm Tatsu Shuwa System

environment

Mac OS 10.15 VSCode 1.39.2 pipenv 2018.11.26 Python 3.7.4 Django 2.2.6

web API specifications

When you request a coupon code with a URL, the contents of the coupon associated with the coupon code will be displayed as a template.

procedure

Make the following modifications to the code created in (for beginners) Create a simple web API with Django.

Register your application to use Django features

Just add the app name (coupon) to INSTALLED_APPS in settings.py under the project name folder.

settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'coupon', #Added line
]

Create html file of template

Create a templates directory under the coupon directory, and then create a coupon directory under the templates directory. (Django's file reference specification recommends duplicating directories in consideration of creating multiple index.html files.)

Create index.html in the created coupon directory. The name of the coupon is tentative and will be Amigo Coupon.

index.html


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>Amigo Coupon</title>
</head>
<body>
    <h1>Amigo Coupon</h1>
    <p>
        <ul>
            <li>Coupon code:{{coupon_code}}</li>
            <li>Benefits:{{coupon_benefits}}</li>
            <li>expiration date:{{coupon_deadline}}</li>
            <li>{{message}}</li>
        </ul>
    </p>
</body>
</html>

Modified views.py so that the render function can send values to the template

Modify as follows.

views.py


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

# Create your views here.
def coupon(request):
    if 'coupon_code' in request.GET:
        coupon_code = request.GET['coupon_code']
        if coupon_code == '0001':
            benefit = '1000 yen discount coupon!'
            deadline = '2019/10/31'
            message = ''
        elif coupon_code == '0002':
            benefit = '10%Discount coupon!'
            deadline = '2019/11/30'
            message = ''
        else:
            benefit = 'NA'
            deadline = 'NA'
            message = 'No coupons available'

        params = {
            'coupon_code':coupon_code,
            'coupon_benefits':benefit,
            'coupon_deadline':deadline,
            'message':message,
        }
        return render(request, 'coupon/index.html', params)

Operation check

After saving your changes, start django's web server and access the URL below with your browser. http://127.0.0.1:8000/coupon/?coupon_code=0001 test-coupon-6a-0001.png

Try changing the coupon_code request to 0002, 0007. test-coupon-6a-0002.png test-coupon-6a-0007.png

that's all.

Next time, I will manage it with git in preparation for future code modifications

Recommended Posts

Try using Django's template feature
Try using Pelican's draft feature
Try using PyCharm's remote debugging feature
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try using virtualenv (virtualenvwrapper)
Try using virtualenv now
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Try using Kubernetes Client -Python-
Feature detection using opencv (corner detection)
Try using the Twitter API
Try using OpenCV on Windows
Try using Jupyter Notebook dynamically
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
Try using SQLAlchemy + MySQL (Part 1)
Try using the Twitter API
Try using SQLAlchemy + MySQL (Part 2)
Try using the PeeringDB 2.0 API
Try using pytest-Overview and Samples-
Organized feature selection using sklearn
Try using folium with anaconda
Try using Janus gateway's Admin API
Try using Spyder included in Anaconda
Try using design patterns (exporter edition)
Try using Pillow on iPython (Part 1)
Simple grid search template using Scikit-learn
Take a look at Django's template.
Try using Pillow on iPython (Part 2)
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using pynag to configure Nagios
Try using ArUco on Raspberry Pi
Try using cheap LiDAR (Camsense X1)
[Sakura rental server] Try using flask.
Try using Pillow on iPython (Part 3)
Reinforcement learning 8 Try using Chainer UI
Try to get statistics using e-Stat
Try using Python argparse's action API
Try Django's official tutorial from scratch
Limit Views using Django's Permission model