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.
Python Django Super Introductory by Yano Palm Tatsu Shuwa System
Mac OS 10.15 VSCode 1.39.2 pipenv 2018.11.26 Python 3.7.4 Django 2.2.6
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.
Make the following modifications to the code created in (for beginners) Create a simple web API with Django.
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 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>
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)
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
Try changing the coupon_code request to 0002, 0007.
that's all.
Next time, I will manage it with git in preparation for future code modifications
Recommended Posts