我正在测试我的项目,我需要测试django表单,但我不知道该怎么做,这是代码
if request.method == 'POST': # If the form has been submitted...
name_add = request.POST.get("name")
form = AddForm(request.POST) # A form bound to the POST datas
not_add = 0
if form.is_valid(): # All validation rules pass
for n in Product.objects.filter(dashboard = curr_dashboard):
if n.name == name_add:
not_add = 1
if not_add != 1:
obj = form.save(commit=False)
obj.dashboard = curr_dashboard
obj.save()
curr_buylist.add_product(obj.id)
return HttpResponseRedirect(request.get_full_path()) # Redirect after POST
else:
forms.ValidationError("You already have this")
return HttpResponseRedirect(request.get_full_path())
我在这里验证它并添加到数据库中。但是我该如何测试呢?这是我在测试中写的
def test_index_form(self):
request = self.factory.post('main/index')
request.user = User.objects.get(username= 'admin')
response = index(request)
self.assertEqual(response.status_code, 200)