2017-02-09 22 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が 'Not Found'を返します。ディレクトリに問題がある可能性はありますか?

+1

通常、URLはスラッシュで始まります。 –

+0

それでは、もう少し私をプッシュしました...今、response.status_codeは304を返し、response.reason_phraseは 'Found'を返します。 ログイン手順が不足している可能性はありますか? – 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')