[PYTHON] I want to create a web application that uses League of Legends data ①

I want to create a LOL data collection web application using Django

Up to the last time, I was able to learn how to retrieve data and save it in a database, so I would like to develop a web application from this part.

Until last time I want to get League of Legends data ① I want to get League of Legends data ② I want to get League of Legends data ③

environment

As of 2/8/2020 OS:windows10 Anaconda:4.8.1 python:3.7.6 MySQL:8.0.19 Django:3.0.1 PyMySQL:0.9.3

Introducing Django

Installation

python -m pip install djangoRun

Change from last time

Change the library to import from MySQLdb to PyMySQL python -m pip install pymysql import mysql -> import pymysql

Create

First, run `django-admin startproject MyProject``` on a directory called APP. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/571147/3df0397c-1103-da55-89a1-1d9ca5622ce4.png) This kind of hierarchy is completed. Execute python manage.py startapp LOLApp `` in the same hierarchy as ** manage.py **. image.png You will have something like this.

Change setting.py

*** Added **'LOLApp.apps.LolappConfig', ** to INSTALLED_APPS ***. Then change *** DATABASES ***. Since the database is sqlite3 by default, make it mysql.

Original code


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
   }
}

After change


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'LOLdb',
        'USER': 'root',
        'PASSWORD': '{Initial password set}',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS':{
            'init_command': "SET 
            sql_mode='STRICT_TRANS_TABLES'",
        }
    }
}

Next, change *** LANGUAGE_CODE *** from **'en-us' ** to **'ja' **. Finally, change *** TIME_ZONE *** from 'UTC' to 'Asia / Tokyo'.

Modify manage.py

At the very end

import pymysql

pymysql.install_as_MySQLdb

Add.

python manage.Run py makemigrations LOL App



#### **`python manage.Run py migrate`**
```Run py migrate

 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/571147/7b645d72-6bac-f99e-f166-0c5fd22b9d31.png)
 In addition to the previously created table ** match_data **, a new one will be created.

## Start the development server

#### **`python manage.Run py run server and try to jump to the output address.`**
```Run py run server and try to jump to the output address.

 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/571147/1fc2fc10-544f-6da0-8d84-0cf645ce03b1.png)
 If such a screen appears, it is a success for the time being.

## Create view

#### **`views.py`**
```python

from django.shortcuts import render
from django.http import HttpResponse #add to
# Create your views here.

def index(request):
    return HttpResponse("Hello World!")

Create *** urls.py *** directly under *** LOLApp ***. Give the path corresponding to the previous view.

LOLApp/urls.py


from django.urls import path
from . import views

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

Make changes to *** urls.py *** directly under *** MyProject ***.

MyProject/urls.py


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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('LOLApp/',include('LOLApp.urls')), #add to
]

python manage.Run py runserver to the previous address**/LOLApp**When you add and access


 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/571147/fd349263-88c0-2bcc-509a-b87449691023.png)
 If it is, it is OK for the time being.

### Create a model


#### **`models.py`**
```python

from django.db import models

# Create your models here.
class Match_data(models.Model):
    sn = models.CharField(max_length=200)
    kills = models.IntegerField(default=0)
    deaths = models.IntegerField(default=0)
    assists = models.IntegerField(default=0)
    championId = models.IntegerField(default=0)
    roles = models.CharField(max_length=200)
    cs = models.IntegerField(default=0)
    gold = models.IntegerField(default=0)
    damage = models.IntegerField(default=0)
    side = models.CharField(max_length=200)
    game_time = models.IntegerField(default=0)

python manage.py makemigrations LOLApp



#### **`python manage.py migrate`**
```py migrate


#### **`python manage.py sqlmigrate LOLApp 0001`**
```py sqlmigrate LOLApp 0001

 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/571147/024e5511-e62f-2e03-2a67-4fac3e7e0041.png)
 ** lolapp_match_data ** is newly added to the table.


#### **`python manage.Run py shell and give it a try.`**
```Run py shell and give it a try.


```python 
from LOLApp.models import Match_data 
Match_data.objects.all()
# <QuerySet []>
a = Match_data(sn="aaa",kills=2,deaths=3,assists=5,championId=100,roles="MID",cs=100,gold=10000,damage=10000,side="BLUE",game_time=1500)
a.save() #Save data
a
#<Match_data: Match_data object (1)>

If you check with the MySQL command image.png It was confirmed that it was saved in the database.

Create an administrative account

python manage.Execute py createsuperuser to register the user name, email address and password.


 You can then run ``` python manage.py runsever``` and go to ** http://127.0.0.1:8000/admin/** to jump to the admin site. Modify *** admin.py *** to work with the model on this administration site.


#### **`admin.py`**
```python

from django.contrib import admin
from .models import Match_data

# Register your models here.
admin.site.register(Match_data)

If you add this and reload it![Image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/571147/e083f106-98cf-feb0-a4a0 -c7de71052209.png) The model has been addedImage.png You can work with the data on this screen.

Summary

This time, I used the framework to modify the data obtained from the previous item. Next, in order to make a WEB application, I have to make a base to display, so I would like to make that a little.

Recommended Posts

I want to create a web application that uses League of Legends data ①
I want to get League of Legends data ③
I want to get League of Legends data ①
Qiskit: I want to create a circuit that creates arbitrary states! !!
I made a web application in Python that converts Markdown to HTML
(Note) A web application that uses TensorFlow to infer recommended song names.
I want to make a web application using React and Python flask
I want to easily create a Noise Model
I want to create a window in Python
I want to create a plug-in type implementation
The story of IPv6 address that I want to keep at a minimum
I want to create a priority queue that can be updated in Python (2.7)
Library for "I want to do that" of data science on Jupyter Notebook
I want to give a group_id to a pandas data frame
I want to install a package of Php Redis
I want to manually create a legend with matplotlib
I want to say that there is data preprocessing ~
(Note) A web application that uses TensorFlow to infer recommended song names [Create an execution environment with docker-compose]
The story of Linux that I want to teach myself half a year ago
(Note) A web application that uses TensorFlow to infer recommended song names [Machine learning]
Create a web application that recognizes numbers with a neural network
I want to start a lot of processes from python
NikuGan ~ I want to see a lot of delicious meat! !!
I want to create a Dockerfile for the time being.
[Python] I wrote a test of "Streamlit" that makes it easy to create visualization applications.
I tried to create a server environment that runs on Windows 10
I want to use a wildcard that I want to shell with Python remove
I tried to create a list of prime numbers with python
I want to create a system to prevent forgetting to tighten the key 1
How to create a large amount of test data in MySQL? ??
I want to create a pipfile and reflect it in docker
I want to create a machine learning service without programming! WebAPI
Created a service that allows you to search J League data
Note that I was addicted to accessing the DB with Python's mysql.connector using a web application.
A story that I had a hard time trying to create an "app that converts images like paintings" with the first web application
I tried to create a linebot (implementation)
I made a web application that converts photos into Van Gogh's style
[Note] I want to completely preprocess the data of the Titanic issue-Age version-
I want to print in a comprehension
I tried to create a linebot (preparation)
[For beginners] I want to get the index of an element that satisfies a certain conditional expression
"CSI" that I want to teach beginners in interactive console application production
I want to build a Python environment
A memorandum of how to write pandas that I tend to forget personally
The story of developing a web application that automatically generates catchphrases [MeCab]
I want to color a part of an Excel string in Python
How to create a wrapper that preserves the signature of the function to wrap
I want to identify the alert email. --Is that x a wildcard? ---
I tried to make a Web API
I tried to perform a cluster analysis of customers using purchasing data
Python: I want to measure the processing time of a function neatly
I tried benchmarking a web application framework
I want to create a machine learning service without programming! Text classification
I made a WEB application with Django
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
What I was addicted to when creating a web application in a windows environment
Go beginner tried to create a cloud native web application using Datastore / GAE
I want to create a nice Python development environment for my new Mac
I tried to make a simple mail sending application with tkinter of Python
I tried to create a class that can easily serialize Json in Python
I made a Line Bot that uses Python to retrieve unread Gmail emails!