Reputation: 3168
I'd like to test adding a custom document file form in my application in the admin panel. Unfortunately django documentation is quite obscure about this one.
This is my model:
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)
This is my test:
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)
I'd like to get 200 response while posting the form with shown data. For some reason response.status_code gives 404 and response.reason_phrase gives 'Not Found'. Is it possible the problem lies in the directory?
Upvotes: 3
Views: 5084
Reputation: 9636
You have to log the client in:
c = Client()
c.login(username='your_username', password='your_password')
response = c.post(change_url, data)
The most correct way to define change_url
is using reverse()
. You can browse the docs to find the correct way to do this. For example:
change_url = reverse('admin:docrepo_document_add')
Upvotes: 7