如何创建django编辑测试和更新测试

时间:2017-01-25 03:14:56

标签: python django unit-testing

I have views.py like this

以及如何创建编辑测试和更新测试?一世 希望编辑和更新可以通过id来完成     来自django.shortcuts导入渲染,重定向     来自.models import People

def index(request):
    peoples = People.objects.all()
    context = {'peoples': peoples}
    return render(request, 'people_app/index.html', context)

def create(request):
    print(request.POST)
    people_app = People(name=request.POST['name'], biography=request.POST['biography'])
    people_app.save()
    return redirect('/')

def edit(request, id):
    people = People.objects.get(id=id)
    context = {'people': people}
    return render(request, 'people_app/edit.html', context)

def update(request, id):
    people = People.objects.get(id=id)
    people.name = request.POST['name']
    people.biography = request.POST['biography']
    people.save()
    return redirect('/')

def destroy(request, id):
    people = People.objects.get(id=id)
    people.delete()
    return redirect('/')

1 个答案:

答案 0 :(得分:0)

在POST请求中执行的编辑或更新操作。由于您具有基于写入定义的视图,因此您必须检查请求的类型并采取相应的行动。

def edit(request, id):
    people = People.objects.get(id=id)
    if request.method == 'POST':
        people.<field_to_update> = request.POST.get('<field_value>')
        people.save()
    context = {'people': people}
    return render(request, 'people_app/edit.html', context)