2017-02-09 109 views
0

我想測試我在管理控制檯應用程序添加自定義文檔文件格式。不幸的是,django文檔對此很模糊。測試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給出了「未找到」。問題出在目錄上嗎?

+1

通常情況下,URL以斜槓開始。 –

+0

好了,把我推遠一點......現在response.status_code返回304和response.reason_phrase給「發現」。 是否有可能是因爲缺失狀態登錄的步驟? – asdfgh

回答

0

你必須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')