Skip to content

Django Cheatsheet

Useful Documentation

  • Topics :
  • Search the Django documentation :
  • Index :
  • How-to :

Using the Console

Change directory to go at the root of the project, where is and type the following command

python3 shell


They go in the following folder


Static files

They go in the following folder


Unit Test

Don't worry if tests: - becomes messy - are redundant

Rules are: - A separate TestClass for each model or view - A separate test method for each set of conditions you want to test - A test method name that describe its function

Creating Tests

To create a test, you must subclass the TestCase class and start its method witht the test prefix. e.g.

class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
     was_published_recently() returns False for questions whose pub_date
     is in the future.
    time = + datetime.timedelta(days=30)
    future_question = Question(pub_date=time)
    self.assertIs(future_question.was_published_recently(), False)

Running Tests

To test the complete django application, launch the following command

python3 test

If you want to test only one module

python3 test <module_name>

Testing the views

Django provides a test client for the views, it can be imported this way

from django.test import Client

and used like

client = Client()
response = client.get('/')
response.status_code  # will return 404 for instance

Also, to make some in browser testing, Django has a LiveServerTestCase to ease the integration of tools such as Selenium.