2017-02-09 2 views
0

관리자 패널의 응용 프로그램에 사용자 정의 문서 파일 양식을 추가하려면 테스트하고 싶습니다. 불행히도 장고 문서는 이것에 대해 아주 모호합니다. 내가 같이 데이터 양식을 게시하는 동안 200 응답을 좀하고 싶습니다Django 관리자 테스트 양식

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) 

:

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) 

이 내 테스트입니다 :

내 모델입니다. 어떤 이유로 response.status_code는 404를 제공하고 response.reason_phrase는 'Not Found'를 제공합니다. 문제가 디렉토리에있을 가능성이 있습니까?

+1

일반적으로 URL은 슬래시로 시작합니다. –

+0

좋아, 그게 좀 더 밀어 ... 지금은 response.status_code 반환 304 및 response.reason_phrase '발견'을 제공합니다. 로그인 단계가 부족하여 가능합니까? – asdfgh

답변

0

당신은 log the client in에 있습니다 reverse()을 사용 change_url을 정의 할 수

c = Client() 
c.login(username='your_username', password='your_password') 
response = c.post(change_url, data) 

가장 올바른 방법입니다. 이 작업을 수행하는 올바른 방법을 찾으려면 browse the docs을 사용할 수 있습니다. 예 :

change_url = reverse('admin:docrepo_document_add')