我想在管理面板的应用程序中测试添加自定义文档文件表单。不幸的是,django的文档对此相当模糊。
这是我的模型:
class Document(models.Model):
pub_date = models.DateTimeField('date published')
title = models.CharField(max_length=255)
description = models.TextField()
pdf_doc = models.FileField(upload_to=repo_dir, max_length=255)这是我的测试:
from django.test import TestCase
from django.test import Client
from django.utils import timezone
from datetime import datetime, timedelta
class DocumentAdminFormTest(TestCase):
def test_add_document_form(self):
client = Client()
change_url = 'admin/docrepo/document/add/'
today = datetime.today()
filepath = u'/filepath/to/some/pdf/'
data = {'title': 'TEST TITLE',
'description': 'TEST DESCRIPTION',
'pub_date0': '2000-01-01',
'pub_date1': '00:00:00',
'pdf_doc': filepath,
}
response = client.post(change_url, data)
print('REASON PHRASE: ' + response.reason_phrase)
self.assertIs(response.status_code, 200)我希望得到200个回应,而张贴的表格显示的数据。出于某种原因,response.status_code给出了404,而response.reason_phrase给出了'Not Found‘。有没有可能问题出在目录上?
发布于 2017-05-26 23:04:28
你必须使用log the client in
c = Client()
c.login(username='your_username', password='your_password')
response = c.post(change_url, data)定义change_url最正确的方法是使用reverse()。你可以通过browse the docs找到正确的方法来做到这一点。例如:
change_url = reverse('admin:docrepo_document_add')https://stackoverflow.com/questions/42127422
复制相似问题