[Django] CRUD with function-based view (fbv)
Published in
2 min readDec 3, 2019
- Using function-based view (fbv) to implement the function.
針對CRUD + List功能使用function-based view來實作
2. 增加Django project and application + settings.py + templates
(略),請自行google查閱。(Django 2.1, Django 1.11)
3. 增加models: Book class
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=20)
pages = models.IntegerField()
def __str__(self):
return self.name
4. 增加forms.py
# forms.py
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
5. List功能
# views.py
from django.shortcuts import render, redirect, get_object_or_404
from .models import Book
from .forms import BookForm
def index(request):
template = 'fbv/fbv_list.html'
book = Book.objects.all()
context = {"book": book}
return render(request, template, context)
5. 增加Create功能
# views.py
def book_create(request):
template = 'fbv/form.html'
form = BookForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('fbv:index')
context = {"form": form}
return render(request, template, context)
6. 增加Update功能
# views.py
def book_update(request, pk):
template = 'fbv/form.html'
book = get_object_or_404(Book, pk=pk)
form = BookForm(request.POST or None, instance=book)
if form.is_valid():
form.save()
return redirect('fbv:index')
context = {"form": form}
return render(request, template, context)
7. 增加Remove功能
def book_delete(request, pk):
template = 'fbv/delete.html'
book = get_object_or_404(Book, pk=pk)
if request.method == 'POST':
book.delete()
return redirect('fbv:index')
context = {"book": book}
return render(request, template, context)
8. 修改urls.py
# urls.py in "django_app/urls.py"
from django.urls import path
from fbv import views
app_name = 'fbv'
urlpatterns = [
path('', views.index, name='index'),
path('add/', views.book_create, name='add'),
path('update/<int:pk>', views.book_update, name='update'),
path('delete/<int:pk>', views.book_delete, name='delete'),
]
9. 修改django_project/urls.py
# urls.py in "django_project/urls.py"
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('fbv/', include('fbv.urls', namespace='fbv')),
]
10. 新增templates/base.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
11. 新增templates/fbv/fbv_list.html, form.html, delete.html
fbv_list.html (在我們的case,django application為fbv)
{% extends 'base.html' %}
{% block title %}
fbv
{% endblock %}
{% block content %}
<a href="{% url 'home' %}">back to home</a><br>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Pages</th>
<th>Activity</th>
</tr>
</thead>
<tbody>
<a href="{% url 'fbv:add' %}">add</a>
{% for b in book %}
<tr>
<td>{{ b.name }}</td>
<td>{{ b.pages }}</td>
<td>
<a href="{% url 'fbv:update' b.id %}">edit</a>
<a href="{% url 'fbv:delete' b.id %}">delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
form.html
{% extends 'base.html' %}
{% block title %}
fbv form
{% endblock %}
{% block content %}
<form class="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="" value="submit">
</form>
{% endblock %}
delete.html
{% extends 'base.html' %}
{% block title %}
{% endblock %}
{% block content %}
<form class="" method="post">
{% csrf_token %}
Are you sure want to delete it?
<input type="submit" name="" value="submit">
</form>
{% endblock %}
12. 更新db、執行/啟動服務,並查看網址 http://localhost:8000/
$ python3.6 manage.py makemigrations
$ python3.6 manage.py migrate
$ python3.6 manage.py runserver
可提供你參考別人的github:
如果你覺得文章很棒或有幫助到您,不妨幫我點個拍手吧! 謝謝…