[PYTHON] (For beginners) Try creating a simple web API with Django

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.

This time [try running python in Django environment made with pipenv] In the environment created by (https://qiita.com/Ajyarimochi/items/0964d314c8bd968fcc80), create a simple web API using Django's functions.

reference

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 a coupon code is requested, the content of the coupon associated with the coupon code is responded by HTTP.

procedure

Create a coupon django app

Enter the pipenv virtual environment with the pipenv shell command and change to the directory where manage.py is located. into-pipenv-directry-mask.png

Create a django app named coupon. make-coupon-app.png

If you open the folder with VS Code, you will have a coupon app. check-making-app-vscode.png

Implemented response processing in views.py

As shown below, I created a function called coupon and hard-coded the contents of the coupon. Depending on the code of the coupon sent by the request variable (coupon_code), the coupon will be sorted by the if statement. The response is returned in Http using Django's HttpResponse method.

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':
          result = '1000 yen discount coupon!'
        elif coupon_code == '0002':
            result = '10%Discount coupon!'
        else:
            result = 'Error:Not found coupon code!'
        return HttpResponse(result)

Create urls.py under coupon app

Next, set the path. First, create a new ʻurls.py` under the coupon app.

coupon/urls.py


from django.urls import path
from . import views

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

Edit urls.py under the project

Next, edit urls.py under the project. Added path ('coupon /', include ('coupon.urls')) to ʻurl patterns`.

ami_coupon_api/urls.py


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

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

Operation check

Save your changes and start django's web server.

$ python manage.py runserver

Access the following URL with a browser. http://127.0.0.1:8000/coupon/?coupon_code=0001 Confirm that the correct response changes. test-code-0001.png

Check the pattern for entering other parameters

Request a coupon with coupon code 0002 http://127.0.0.1:8000/coupon/?coupon_code=0002 test-code-0002.png

If you specify a coupon code that does not exist http://127.0.0.1:8000/coupon/?coupon_code=0003 test-code-0003.png

With the above, we have implemented a super-simple web API. From here, we will modify it as follows to make it a web API that can withstand practical use.

Next time, I'll try using Django's template feature for learning [https://qiita.com/Ajyarimochi/items/9d64b13321f0779fc986)

Recommended Posts

(For beginners) Try creating a simple web API with Django
[For beginners] Try web scraping with Python
Looking back on creating a web service with Django 1
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Creating a simple app with flask
Build a web application with Django
Create a web API that can deliver images with Django
Commands for creating SNS with Django
Create a social integration API for smartphone apps with Django
Rails users try to create a simple blog engine with Django
Creating a simple PowerPoint file with Python
A simple RSS reader made with Django
Commands for creating a new django project
Creating a login screen with Django allauth
Create a simple web app with flask
I made a WEB application with Django
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Procedure for creating a LineBot made with Python
Start a simple Python web server with Docker
Create a dashboard for Network devices with Django!
Commands for creating a python3 environment with virtualenv
A simple to-do list created with Python + Django
Try creating a FizzBuzz problem with a shell program
A simple workaround for bots to try to post tweets with the same content
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Try making a simple website with responder and sqlite3
(Python) Try to develop a web application using Django
I tried a simple RPA for login with selenium
[Vagrant] Set up a simple API server with python
Build a Django environment for Win10 (with virtual space)
Develop a web API that returns data stored in DB with Django and SQLite
Beginners try to make an online battle Othello web application with Django + React + Bootstrap (1)
Try running python in a Django environment created with pipenv
Create an API with Django
Django beginners create simple apps 3
[Memo] Build a development environment for Django + Nuxt.js with Docker
Try programming with a shell!
Django beginners create simple apps 1
Recommendations for django, wagtail ~ Why develop a website with python ~
Create a homepage with django
Set up a web server with CentOS7 + Anaconda + Django + Apache
Try creating a CRUD function
Web application creation with Django
Try morphological analysis and Markov chains with Django (Ari with a lot of room for improvement)
Web API with Python + Falcon
Build a speed of light web API server with Falcon
Django beginners create simple apps 2
Django beginners make simple apps 4
Tornado-Let's create a Web API that easily returns JSON with JSON
Real-time web with Django Channels
Qiita API Oauth with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Django beginners create simple apps 5
Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
"2/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
If you know Python, you can make a web application with Django
Let's make a WEB application for phone book with flask Part 1
Rubyist tried to make a simple API with Python + bottle + MySQL
[Python / Django] Create a web API that responds in JSON format
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)