【发布时间】:2020-03-04 02:31:52
【问题描述】:
我正在尝试对表单进行测试。它一直返回无效。不过其他形式也可以。
forms.py中的表单:
class SongForm(forms.ModelForm):
class Meta():
model = Song
fields = ('title', 'type', 'audio_file', 'description', 'written_by', 'song_text')
models.py中表单来自的模型:
class Song(models.Model):
title = models.CharField(max_length=120)
SONG_TYPES = (
('KI', 'Kiirtan'),
('PS', 'Prabhat Samgiita'),
('BH', 'Bhajan'),
)
type = models.CharField(max_length=30, choices=SONG_TYPES)
capo = models.PositiveIntegerField(blank=True, null=True, validators=[MaxValueValidator(12)])
description = models.TextField(blank=True)
uploader = models.ForeignKey('Profile', null=True, on_delete=models.SET_NULL)
written_by = models.CharField(max_length=50, blank=True)
song_text = models.TextField(blank=True)
audio_file = models.FileField(upload_to='songs/')
upload_date = models.DateField(auto_now=False, auto_now_add=True)
edit_date = models.DateField(auto_now=True, auto_now_add=False)
chords = models.ManyToManyField('Chord', related_name='chords', through='ChordIndex')
tests/test_forms.py 中的测试:
def test_Song_form_is_valid(self):
the_data = {
'title': 'a song',
'type': 'KI',
'description': '',
'audio_file': '103_VASANTA_AJ_JAGALO_04Tsyry.mp3'
'written_by': '',
'song_text': '',
}
form = SongForm(data=the_data)
self.assertTrue(form.is_valid())
表单在使用 POST 方法的 Web 应用程序中正常工作。我什至尝试从测试中的有效 POST 中复制值,但仍然失败!
我已经被这个问题困扰了一段时间......有什么线索吗? ??????????
【问题讨论】:
-
我的问题是使用 FileField 测试表单。这篇文章为我解决了这个问题:stackoverflow.com/questions/2473392/…
标签: python django unit-testing testing