Looking back on creating a web service with Django 1

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

This and that used in models.py

Example:

class Profile(models.Model):
~~ Field definition omitted ~~
	
	@receiver(post_save, sender=User)
	def create_profile(sender, **kwargs):
		if kwargs['created']:
			user_profile = Profile.objects.get_or_create(user=kwargs['instance'])
	
	def __str__(self):
		return str(self.user)

@receiver @receiver is a signal, a function that calls the registered process when an event occurs. The post_save used in this example means that it will be executed immediately after adding or changing a record for the model (User in this case) specified by sender.

**kwargs You can specify any number of keyword arguments by defining arguments with **, such as ** kwargs. In the function, it is received as a dictionary whose argument name is key key and value is value. In this example, we take the sender as an argument and check if the instance was created with the if statement.

get_or_create If the object does not exist, it will be registered, and if it exists, it will not be registered.

def str(self) Process called when str or print is applied to the instance

This and that used in forms.py

Example:

class CommentCreateForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

    class Meta:
        model = Comment
        fields = ('text',)

*args It means whether to take a variable length argument as a tuple type or a dict type.

class Meta class Meta is a place to define something that is not a field definition.

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
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)
Deploy a Django application with Docker
Creating a voice transcription web application
Django Tips-Create a ranking site with Django-
Create a SlackBot service on Pepper
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
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
Play like a web app with ipywidgets
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