[PYTHON] Looking back on creating a web service with Django 2

I made a web service with Django. It was completed, but I checked again where it was fluffy.

This and that used in views.py

What I used in class view

Example:

views.py


class ThreadView(ModelFormMixin, generic.DetailView):
	model = Group
	form_class = CommentCreateForm

	def form_valid(self, form):
		group_pk = self.kwargs['pk']
		comment = form.save(commit=False)
		comment.create_user = self.request.user
		comment.post = get_object_or_404(Group, pk=group_pk)
		comment.save()
		return redirect('<app name>:group_thread', pk=group_pk)
		
	def post(self, request, *args, **kwargs):
		form = self.get_form()
		if form.is_valid():
			return self.form_valid(form)
		else:
			self.object = self.get_object()
			return self.form_invalid(form)

form.save(commit=False) Returns an instance of the Model object associated with the form. Do not register in DB

What was used in the function view

Page transition method following return

I used render, but I used it properly

HttpResponse Hand over strings and iterators (and it's officially written, but I haven't used it so I can't really imagine it.)

render The render method basically takes three arguments: request, template_name, and context. request: get or post template_name: html file context: Information on the data stored in the DB In the description of the official document, "Load the template, populate the context, and return the result of rendering the template in an HttpResponse object"

redirect Discard the data returned by POST and transition to a different view

Recommended Posts

Looking back on creating a web service with Django 1
Looking back on creating a web service with Django 2
Build a web application with Django
(For beginners) Try creating a simple web API with Django
Creating a login screen with Django allauth
A note on enabling PostgreSQL with Django
Create a web service with Docker + Flask
Launched a web application on AWS with django and changed jobs
I made a WEB application with Django
Looking back on learning with Azure Machine Learning Studio
Looking back on ABC155
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Deploy a Python 3.6 / Django / Postgres web app on Azure
Deploy a Django app made with PTVS on Azure
Launch Django on a Docker container with docker-compose up
Notes on creating a virtual environment with Anaconda Navigator
Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
Looking back on iOS'Healthcare App' 2019
Create a homepage with django
Web application creation with Django
Real-time web with Django Channels
Set up a web server with CentOS7 + Anaconda + Django + Apache
(Failure) Deploy a web app made with Flask on heroku
Create a web API that can deliver images with Django
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
3. Natural language processing with Python 3-4. A year of corona looking back on TF-IDF [Data creation]
Looking back on Python 2020 around me
If you know Python, you can make a web application with Django
Creating a web application using Flask ②
Creating a decision tree with scikit-learn
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Creating a Flask server with Docker
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Creating a voice transcription web application
Django Tips-Create a ranking site with Django-
Implement a Django app on Hy
Creating a simple app with flask
Creating a web application using Flask ①
Make a filter with a django template
Commands for creating SNS with Django
Creating a web application using Flask ③
Creating a web application using Flask ④
Create a file uploader with Django
[Spotify API] Looking back on 2020 with playlists --Part.1 Acquisition of playlist data
Looking back on the 10 months before a programming beginner becomes a Kaggle Expert
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
A series of amateur infrastructure engineers touching Django with Docker (2): Creating a model
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Until you publish (deploy) a web application made with bottle on Heroku
[Spotify] Looking back on 2020 with playlists --Part.2 EDA (basic statistics), data preprocessing
Publish a web application for viewing data created with Streamlit on heroku
Creating a simple PowerPoint file with Python
A simple RSS reader made with Django
Notes on creating static files in Django
Make Jupyter Notebook a service on CentOS
Commands for creating a new django project
Django + Apache with mod_wsgi on Windows Server 2016
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Make Unity Accelerator a service on Linux
Throw a request with a certificate on httpie