python - 如何使用测试客户端填写 Django 表单
问题描述
我想测试我的 Django 表单,但我收到了这个错误
django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']
这样做:
self.client.post(self.url, {"title" : 'title', "status" : 2, "user" :1})
而我的模型只需要那些字段......
谢谢 :)
编辑1:这是表格:
class ArticleAdminDisplayable(DisplayableAdmin):
fieldsets = deepcopy(ArticleAdmin.fieldsets)
list_display = ('title', 'department', 'publish_date', 'status', )
exclude = ('related_posts',)
filter_horizontal = ['categories',]
inlines = [ArticleImageInline,
ArticlePersonAutocompleteInlineAdmin,
ArticleRelatedTitleAdmin,
DynamicContentArticleInline,
ArticlePlaylistInline]
list_filter = [ 'status', 'keywords', 'department', ]
class ArticleAdmin(admin.ModelAdmin):
model = Article
关于文章模型有太多的继承,所以你必须相信我,(模型)需要的唯一字段是标题、状态和用户。
解决方案
好的,所以从你的判断来看form
,你有很多正在使用的 django 插件。我应该要求你完整的test
,但我想我可能明白一些问题出在哪里。
当您 时self.client.post
,您实际上是在检查view
而不一定是表格。{"title" : 'title', "status" : 2, "user" :1}
是您的 3 个client
值post
。
Input Field : Data(Value of the Field)
title :'title' # A string
status : 2 # The number 2
user : 1 # The number 1
这是一些对我有用的测试代码。希望它可以帮助你。
表格.py
from .models import CustomerEmployeeName
class EmployeeNameForm(ModelForm):
class Meta:
model = CustomerEmployeeName
fields = [
'employee_choices',
'first_name',
'middle_name',
'last_name',
]
test_forms.py
from django.test import TestCase
from .forms import EmployeeNameForm
class TestEmployeeNameForm(TestCase):
"""
TESTS: form.is_valid
"""
# form.is_valid=True
# middle_name not required.
# middle_name is blank.
def test_form_valid_middle_optional_blank(self):
name_form_data = {'first_name': 'First', # Required
'middle_name': '', # Optional
'last_name': 'Last', # Required
'employee_choices': 'E', # Required
}
name_form = EmployeeNameForm(data=name_form_data)
self.assertTrue(name_form.is_valid())
视图.py
from .forms import EmployeeNameForm
def create_employee_profile(request):
if request.POST:
name_form = EmployeeNameForm(request.POST)
if name_form.is_valid():
new_name_form = name_form.save()
return redirect(new_name_form) #get_absolute_url set on model
else:
return render(request,
'service/template_create_employee_profile.html',
{'name_form': name_form}
)
else:
name_form = EmployeeNameForm(
initial={'employee_choices': 'E'}
)
return render(request,
'service/template_create_employee_profile.html',
{'name_form': name_form}
)
test_views.py
from django.test import TestCase, Client
from service.models import CustomerEmployeeName
class TestCreateEmployeeProfileView(TestCase):
# TEST: View saves valid object.
def test_CreateEmployeeProfileView_saves_valid_object(self):
response = self.client.post(
'/service/', {
'first_name': 'Test', # Required
'middile_name': 'Testy', # Optional
'last_name': 'Testman', # Required
'employee_choices': 'E', # Required
})
self.assertTrue(CustomerEmployeeName.objects.filter(
first_name='Test').exists())
如果您想发布更多代码,我将很乐意查看它。
推荐阅读
- node.js - WebStorm Salesforce Commerce Cloud 测试错误 - 'C:\Users\...' 未被识别为内部或外部命令
- mlab - Mlab 在 ubuntu 20.04 上呈现黑屏
- javascript - 调用多个函数 onChange 事件——React
- python - deeplab tensorflow model_test.py 工作正常,但在 main 中看不到导入
- core-location - 如何在 Mac Catalyst 上测试核心位置授权提示?
- reactjs - AWS S3 存储桶策略/来自 URL 引用者的访问 - 访问被拒绝
- r - 我的 ggplot 工作时没有闪亮,但是当我使用闪亮时,它不会在没有任何错误的情况下呈现
- python-3.x - 无法识别 Docker 容器中给定目录中的 SQL 脚本
- c# - 如何使用 JsonSerializer 为日期指定默认时区?
- c++ - 使用二叉搜索树时的 C++ 堆栈溢出