Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 测试包含ImageField的表单,导致类型错误_Django_Django Forms - Fatal编程技术网

Django 测试包含ImageField的表单,导致类型错误

Django 测试包含ImageField的表单,导致类型错误,django,django-forms,Django,Django Forms,我正在尝试测试一个包含ImageField的表单,但遇到了一个错误。我尝试过单步执行Django代码,但我一直迷路,看不到问题所在。我已经研究过如何做到这一点,我认为我做的每件事都是正确的 以下是测试中assertTrue语句中出现的错误: TypeError: is_valid() takes exactly 2 arguments (1 given) 这是我的档案: # forms.py class UploadMainPhotoForm(forms.Form): photo =

我正在尝试测试一个包含ImageField的表单,但遇到了一个错误。我尝试过单步执行Django代码,但我一直迷路,看不到问题所在。我已经研究过如何做到这一点,我认为我做的每件事都是正确的

以下是测试中assertTrue语句中出现的错误:

TypeError: is_valid() takes exactly 2 arguments (1 given)
这是我的档案:

# forms.py
class UploadMainPhotoForm(forms.Form):
    photo = forms.ImageField(error_messages={'required': err_msg__did_not_choose_photo})

    def is_valid(self, request):

        # Run parent validation first
        valid = super(UploadMainPhotoForm, self).is_valid()
        if not valid:
            return valid

        if request.FILES:
            photo_file = request.FILES['photo']
            file_name, file_ext = os.path.splitext(photo_file.name)
        else:
            self._errors['photo'] = 'You forgot to select a photo.'
            return False

        # Return an error if the photo file has no extension
        if not file_ext:
            self._errors['photo'] = err_msg__photo_file_must_be_jpeg
            return False

        return True

# upload_photo.html (template)
<form method="post" enctype="multipart/form-data" action=".">{% csrf_token %}
    <input type="file" name="photo" />
    <input type="submit" name="skip_photo" value="Skip Photo" />
    <input type="submit" name="upload_photo" value="Upload Photo">
</form>

# form_tests.py
class TestUploadMainPhotoForm(TestCase):
    def initial_test(self):
        post_inputs = {'upload_photo': '[Upload Photo]'}
        test_photo = open('photo.jpg', 'rb')
        file_data = {'file': SimpleUploadedFile(test_photo.name, test_photo.read())}
        self.assertTrue(UploadMainPhotoForm(post_inputs, file_data).is_valid())
在这里:

您已经使用参数请求定义了is_valid方法,并且在调用它时

self.assertTrue(UploadMainPhotoForm(post_inputs, file_data).is_valid())
您尚未指定参数。因此出现了错误

您可以使用获取unittest中的请求对象

from django.utils import unittest
from django.test.client import RequestFactory

class TestUploadMainPhotoForm(TestCase):
    def setUp(self):
        self.request = RequestFactory()

    def initial_test(self):
        post_inputs = {'upload_photo': '[Upload Photo]'}
        test_photo = open('photo.jpg', 'rb')
        file_data = {'file': SimpleUploadedFile(test_photo.name, test_photo.read())}
        self.assertTrue(UploadMainPhotoForm(post_inputs, file_data).is_valid(self.request))

谢谢出于某种原因,我认为您必须始终具有'request'参数。请原谅我犯了这样一个愚蠢的错误。这是一个诚实的错误,而且确实发生了伙计,你太棒了!非常感谢。读了你的答案后,我意识到我遇到了一个新问题,那就是如何在测试中获取请求对象。我挣扎了将近一个小时才回来刷新这个页面,看到了您添加的代码。我真的从这个问题中学到了很多。再次感谢!
from django.utils import unittest
from django.test.client import RequestFactory

class TestUploadMainPhotoForm(TestCase):
    def setUp(self):
        self.request = RequestFactory()

    def initial_test(self):
        post_inputs = {'upload_photo': '[Upload Photo]'}
        test_photo = open('photo.jpg', 'rb')
        file_data = {'file': SimpleUploadedFile(test_photo.name, test_photo.read())}
        self.assertTrue(UploadMainPhotoForm(post_inputs, file_data).is_valid(self.request))