5(write to database)

修改views.py

from django.shortcuts import render
from .forms import AppNameForm
# Create your views here.
def home(request):
    title='home'
    #没有括号里的参数就不能写入数据库,还不能运行clean_email
    form = AppNameForm(request. POST or None )
    context = {
            "title" : title,
            "form" : form,
        }
    if form.is_valid():
        instance = form.save(commit=False)
        if not instance.attr:#在这里修改值
            instance.attr = "default attr"
        instance.save()#写入数据库
        context = {
            "title" : "thank you",
        }
    return render(request, "home.html",context)

你可能感兴趣的:(5(write to database))