1
我測試我的項目,我需要測試Django的形式,但我不知道該怎麼做,這裏的代碼的Django/Python測試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)