我有一个提交帖子的视图,它包含一个使用CKEditor作为该表单一部分的表单,这是 forms.py
中的表单class CreateAdminPostForm(forms.ModelForm):
content = forms.CharField(widget=CKEditorUploadingWidget())
title = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Post Title',
'class': 'form-control',
'style': 'max-width: 800px;',
'maxlength': '160',
}
)
)
keyword = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Focus Keyword',
'maxlength': '60',
}
)
)
slug = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Slug',
'maxlength': '60',
}
)
)
description = forms.CharField(
widget=forms.Textarea(
attrs={
'placeholder': 'Post Description , Will be used as the Meta description for the post page',
'style': 'height: 370px;',
'maxlength': '160',
}
)
)
class Meta:
model = Post
fields = ['title', 'content', 'slug', 'keyword', 'category', 'description', 'featured_image']
这是我在 views.py
中使用的视图def admin_post(request):
current_user = request.user
current_author = Author.objects.get(user=current_user)
create_admin_post_form = CreateAdminPostForm(request.POST, request.FILES)
if request.method == 'POST':
if create_admin_post_form.is_valid():
form = create_admin_post_form.save(commit=False)
form.active = True
form.email_active = True
form.author = current_author
form.save()
return redirect('home')
else:
create_admin_post_form = CreateAdminPostForm()
context = {
'create_admin_post_form': create_admin_post_form,
}
return render(request, 'admin_post.html', context)
最后,这就是我在测试
中对其进行测试的方式class TestAdminPost(TestCase):
def test_admin_post_view(self):
response = self.client.get(reverse('admin_post'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'admin_post.html')
def test_admin_post_empty(self):
response = self.client.post(reverse('register'), {})
self.assertFormError(response, 'create_admin_post_form', 'content', 'This field is required.')
self.assertFormError(response, 'create_admin_post_form', 'title', 'This field is required.')
self.assertFormError(response, 'create_admin_post_form', 'slug', 'This field is required.')
self.assertFormError(response, 'create_admin_post_form', 'keyword', 'This field is required.')
self.assertFormError(response, 'create_admin_post_form', 'category', 'This field is required.')
self.assertFormError(response, 'create_admin_post_form', 'description', 'This field is required.')
self.assertFormError(response, 'create_admin_post_form', 'featured_image', 'This field is required.')
def test_admin_post_valid(self):
content = 'this is a test content for the post'
title = ' also this is a test title for the great post '
slug = 'test-slug'
keyword = 'test keyword'
category = Category.objects.create(name='testing category', slug='testing-category')
description = 'this is the demo description for the test post'
image = SimpleUploadedFile(name='test_image.jpg', content=open('author.png', 'rb').read(),
content_type='image/png')
response = self.client.post(reverse('register'),
{'content': content, 'title': title, 'slug': slug, 'keyword': keyword,
'category': category, 'description': description, 'image': image})
self.assertEqual(response.status_code, 302)
在测试中,我使用了get
请求来测试模板和响应,然后我使用了2个post
请求,其中一个包含空数据,另一个具有有效数据
尝试测试时出现此错误:
我在使用我的CKEditor self.assertFormError(response, 'create_admin_post_form', 'content', 'This field is required.')
的第AssertionError: The form 'create_admin_post_form' was not used to render the response
行出现了错误
来自test_admin_post_empty
部分
也收到此错误
AssertionError: 200 != 302
来自test_admin_post_valid
错误