Django function-based view

Introduction

Here's the basics of django's function-based views. Write in views.py under the application directory. Suppose you want to use SampleModel in models.py as a model.

View function for list page

This function can be used for pages that list multiple records, such as the article list page of a blog.

views.py


from django.shortcuts import render

from .models import SampleModel


def list_func(request):
    object_list = SampleModel.objects.all()
    context = {'object_list': object_list}
    return render(request, 'app/app_list.html', context)

ʻObject_listis a list of all records contained in the model to be displayed. contextis a dictionary that shows the correspondence between variable names when embedding variables in a template HTML file and variable names in this function. Then, in the finalrender function, specify the HTML file that will be the template and the contextmentioned above (the first argument will berequest`).

View function for newly created page

This function can be used for pages that create new records, such as creating new articles for blogs.

views.py


from django.shortcuts import render, redirect

from .models import SampleModel


def create_func(request):
    if request.method == 'POST':
        object = SampleModel.objects.create(
                title = request.POST['title'],
                text = request.POST['text']
                )
        object.save()
        return redirect('app_list')
    else:
        return render(request, 'app/app_create.html')

First, at ʻif request.method =='POST', the processing when POST is sent is described. Use the Model.objects.create method to create a new record (here ʻobject) according to the content of the request sent by POST, and register it in the database with thesave ()method. If the writing to the database is successful, the page specified by redirect () will be displayed. If the page is called with GET instead of POST (for example, if it is accessed by typing the URL normally), the template HTML file specified by render () will be called.

View function for detail page

A function that can be used on pages that display record details, such as individual pages of blog posts.

views.py


from django.shortcuts import render

from .models import SampleModel


def detail_func(request, pk):
    object = SampleModel.objects.get(pk=pk)
    context = {'object': object}
    return render(request, 'app/detail.html', context)

ʻObject is the record that is extracted from the record registered in the model (pk is the primary key). The information related to ʻobject is embedded in the variable of the template HTML file and displayed.

View function for edit page

This function can be used for pages that update the contents of records once created, such as the edit screen of blog articles.

views.py


from django.shortcuts import render, redirect

from .models import SampleModel


def update_func(request, pk):
    object = SampleModel.objects.get(pk=pk)
    context = {'object': object}
    if request.method == 'POST':
        object.title = request.POST['title']
        object.text = request.POST['text']
        object.save()
    else:
        return render(request, 'app/app_update.html', context)

As with the view introduced above, specify the target record with ʻobject and specify the variable name to be embedded in the HTML template file with context`. When the update is POSTed, it overwrites the contents of each field in the database.

View function for deleted pages

This function can be used for pages that delete created records, such as the delete screen for blog articles.

views.py


from django.shortcuts import render, redirect

from .models import SampleModel


def delete_func(request, pk):
    object = SampleModel.objects.filter(pk=pk)
    context = {'object': object}
    if request.method == 'POST':
        object.delete()
        return redirect('app:app_list')
    else:
        return render(request, 'app/app_delete.html', context)

ʻObject and contextare similar to the views introduced above. Use thedelete` method to delete a record from the database.

Summary

Here's the basics of django's function-based views. Next time, I will explain about urls.py.

Recommended Posts

Django function-based view
Django class-based view
Django2 View configuration pattern
Django
Django note 4
Django memorandum
Django installation
Django Summary
Django test
Django # 2 (template)
Django Note 5
Django tutorial summary for beginners by beginners ③ (View)
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Ajax in Django (using generic class view)
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
Output Django Detail View as PDF (Japanese support)
DJango Memo: From the beginning (creating a view)
Pass login user information to view in Django
Django tutorial summary for beginners by beginners ④ (Generic View)
Specify the view URL in your Django template