def categoryCreate(request):
if request.method == 'GET':
return render(request, 'store/categoryCreate.html')
elif request.method == 'POST':
name = request.POST.get('name')
category = Category()
category.name = name
category.save()
return redirect('/store/category/')
在django form 表單如果是post 需要再多 csrf_token 欄位
store/templates/store/categoryCreate.htnl
<!doctype html>
<head></head>
<h3>新增類別</h3>
<form method="post" action="/store/categoryCreate/">
{% csrf_token %}
<label for="name">名稱</label>
<input id="name" name="name">
<input type="submit" value="儲存">
</form>
</body>
</html>
store/templates/store/category.htnl
<a href="/store/categoryCreate/">新增</a>
今天就完成了新增資料
明天再講重購的部分,還有驗證 等等