[GO] Web application development memo in python

Introduction

After learning scraping with python, I wanted to make a web application using the acquired data, so this is a memo that I investigated how to realize it.

Which framework is better?

A framework called Django is recommended.

--Reference article: Developing a web application with Python! Introducing popular frameworks

Learn more about Django.

-Django Document

How to install Django

See below [Introduction to Django] Install Django on your computer!

When I tried to run $ pip install django, it said that the version of pip itself was old and needed to be updated ...

--Reference article: Python Tips: I want to update pip itself

Did not launch python shell with $ python in Git Bash. It seems to be a specification ... It seems better not to handle python with Git Bash (execute quietly at the command prompt)

--Reference article: What to do if Python doesn't work on Git for Windows

Creating projects and applications

--Reference article: [Introduction to Django] Django App Design Philosophy! Learn MTV models through models!

The command to create a project is as follows

django-admin startproject mySite

The command to create a template is as follows

python manage.py startapp myapp

By using the function called model, it seems that you can mess with the database without directly touching the DB (I wanted to make it with a three-tiered architecture, so I am grateful that the DB can be operated easily)

How to use template

--Reference article: [Introduction to Django] Let's display a web page using template and HTML

First, add the following description of 'DIRS' in TEMPLATES in the settings.py file.

os.path.join(BASE_DIR, 'templates'),

This declares "put the template file in a directory called templates" (apparently).

After that, as a flow to create a template display process after creating a templates directory

--Definition of view function --Linking URL and view function (for each project and application)

Feeling like that.

After tying

python manage.py runserver

You can run the command and start the server on the local host to see if the template is displayed.

The django template has a lot of useful features, so I wanted to use it. See the official documentation (https://docs.djangoproject.com/ja/3.1/topics/templates/) for more information.

A series of steps from scraping to display based on the data entered on the web page

Reference article: python: Let's make a web application (scraping with django)

It will be described in various ways in the views.py file

The concept of view

view is responsible for the function of "acquiring the page/data to be displayed and generating a response based on the request from the browser" in django, which adopts the design concept of MTV (Model, Template, View).

--Reference article: Understanding MTV

And django has a feature called "general view" that sets the appropriate default values ​​for your use case.

--Reference article: Introduction to class-based general-purpose views in Django and sample usage

See below for how to use different types of general-purpose views

--Reference article: -Introduction to class-based generic views in Django and sample usage -Collect Django's generic class views and mention the implementation

This time, I want to run scraping based on the data input by the web page, so I will use CreateView which can use form.

About the reverse/reverse_lazy function

When specifying the success_url variable of CreateView (URL of the page that transitions when the creation is successful), use a function called reverse_lazy.

Functions such as reverse and reverse_lazy are used when "calling a url by name".

--Reference article: Easy-to-understand explanation of how to use reverse [with specific code]

Specifically, when reverse ('home') is set, the url specified as name ='home' in urlpatterns is called.

The difference between reverse and reverse_lazy is that "if you want to use the reverse function before reading URLConf (= urls.py), use reverse_lazy".

--Reference article: [Django] success_url and get_success_url and reverse and reverse_lazy properly used

When using it in a class, it seems to correspond to "before reading URLConf (= urls.py)", so use revese_lazy.

It seems that in order to understand lazy evaluation correctly, it is necessary to understand what is evaluated in python at what timing, but honestly I do not know well ...

--Reference article: Difference between reverse () and reverse_lazy () in Django

List and append function in python

Reference article: [Python] Let's understand how to use append! Thorough explanation of roles and practical methods!

You can add a single element to an existing list by using the append function.

About the render function

Reference article:

-Python Django Tutorial (3) -Official Document

A function that returns the rendering result using that template as HttpResponse when you pass request and template path. By passing a dictionary type value as the third argument, you can pass the value to template.

⇒ Now you can use the append function to pass the element (obtained by scraping) that is inserted into the dictionary type list to the template and fit it as an element in HTML

About model in django

--Reference: Official Document

One Manager is added by default to the model class.

--Function: Provides an interface for DB operations --Name: objects (Why isn't it a manager ...?)

The model manager has various methods.

--.objects.all (): Returns all elements in the DB --. Objects.filter (): Return with specified conditions

Note: .objects is required to call the methods that the model manager has. Reference: [For beginners] Understand when objects are required / not required for Django model operations

Recommended Posts

Web application development memo in python
Web application development in Go language_Hands-on day 1
Framework development in Python
Development environment in Python
Slackbot development in Python
Steps to develop a web application in Python
WEB application development using django-Development 1-
Web scraping notes in python3
Web application development with Flask
Python development in Visual Studio 2017
[MEMO] [Development environment construction] Python
Web application with Python + Flask ② ③
Python development in Visual Studio
Web application with Python + Flask ④
WEB application development using Django [Django startup]
python web scraping-get elements in bulk
WEB application development using Django [Application addition]
Python memo
Automatic minimum testing in web development
python memo
Python memo
GitHub x Python development preparation memo
python memo
Get, post communication memo in Python
Hit the web API in Python
Vim + Python development environment setting memo
Python memo
Function synthesis and application in Python
Emacs Python development environment construction memo
web coder tried excel in Python
Application development with Docker + Python + Flask
Python memo
Python memo
Learning history for participating in team application development in Python ~ Index page ~
[Pytest] [mock] Web development beginners summarized unit test and mock in python.
[Python] Web application from 0! Hands-on (2) -Hello World-
[Python] Web application from 0! Hands-on (3) -API implementation-
WEB application development using Django [Model definition]
WEB application development using Django [Initial settings]
Display matplotlib diagrams in a web application
WEB application development using Django [Request processing]
[Python] Web application from 0! Hands-on (4) -Data molding-
WEB application development using Django [Template addition]
[Python] A quick web application with Bottle!
[Python] Web application design for machine learning
Run a Python web application with Docker
Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
[Python] Web application from 0! Hands-on (0) -Environmental construction-
About Cloud run + Firebase development [Web application development]
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
Quadtree in Python --2
Python in optimization
CURL in python
[Python] Memo dictionary
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
python beginner memo (9.2-10)
Meta-analysis in Python