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.
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 final
render function, specify the HTML file that will be the template and the
contextmentioned above (the first argument will be
request`).
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.
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.
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.
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 the
delete` method to delete a record from the database.
Here's the basics of django's function-based views. Next time, I will explain about urls.py.
Recommended Posts