热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在django中测试表格?-howtotestformsindjango?

Mymodelslookslikethis:我的模型看起来像这样:classCar(models.Model):idmodels.UUIDField(primary_

My models looks like this:

我的模型看起来像这样:

class Car(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    name = models.CharField(max_length=200)

class Owner(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    car = models.ForeignKey(Car)

and a form that looks like this

和一个看起来像这样的形式

class CarForm (forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(CarForm, self).__init__(*args, **kwargs)
        self.fields['car_name']=forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}),label='', required=False)
        self.fields['person_name']=forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}),label='', required=False)

So when the user goes to my index file he find two forms, one for his name and one for the car's name which when submitted will be created in the database then.

因此,当用户访问我的索引文件时,他会找到两个表单,一个用于他的名字,另一个用于汽车的名称,然后在数据库中创建。

So now I am in the shell and want to test that and I'm not sure what the correct sytnax is, I tried this:

所以现在我在shell中并且想要测试它并且我不确定正确的sytnax是什么,我试过这个:

respOnse= client.post('/', {'car_name':'something','person_name':'something'})

but I always get a

但我总是得到一个

IndexError: list index out of range

IndexError:列表索引超出范围

What does that mean? Or what's the correct syntax to run the tests?

那是什么意思?或者运行测试的正确语法是什么?

I also tried

我也试过了

respOnse= client.post('/', {'id_car_name':'something','id_first_name':'something'})

Since these are the ids that django creates in the homepage, but didn't work

因为这些是django在主页中创建的ID,但是没有用

1 个解决方案

#1


1  

Your test case syntax looks correct. The field names from your first example are correct and match those declared in self.fields. Here is a more detailed example (with assertions as desired):

您的测试用例语法看起来正确。第一个示例中的字段名称是正确的,并与self.fields中声明的字段名称匹配。这是一个更详细的示例(根据需要使用断言):

from django.test import Client, TestCase

class CarFormTest(TestCase):
    def test_form(self):
        client = Client()
        respOnse= client.post('/', {'car_name':'something','person_name':'something'})
        self.assertEqual(response.status_code, 200)

I don't see anything in your test line that would cause an IndexError.

我在测试行中没有看到任何会导致IndexError的内容。

Given that you did not map the Car model to the CarForm like this:

鉴于您未将Car模型映射到CarForm,如下所示:

class CarForm(forms.modelForm):
    class Meta:
        model = Car

It appears that you don't want a direct mapping and instead you need a custom save() method on your CarForm to create Car and Owner objects when you save the form. The IndexError must be coming from a line of code that you have not shown.

您似乎不需要直接映射,而是需要在CarForm上使用自定义save()方法在保存表单时创建Car和Owner对象。 IndexError必须来自您未显示的一行代码。

Please update your question to provide additional code.

请更新您的问题以提供其他代码。


推荐阅读
author-avatar
华力
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有