djangodocs.org

669 sections in Django 5.1 Search all versions →

Using class-based views

Topic guide Introduction to class-based views

… is worth noting that what your method returns is identical to what you return from a function-based view, namely some form of HttpResponse. This means that http shortcuts or TemplateResponse objects are valid to use inside a class-based view. While …

Topic guide Models FieldsRelationships

It doesn’t matter which model has the ManyToManyField, but you should only put it in one of the models – not both. Generally, ManyToManyField instances should go in the object that’s going to be edited on a form.

Topic guide Time zones Concepts

Most websites that care about time zones ask users in which time zone they live and store this information in the user’s profile. For anonymous users, they use the time zone of their primary audience or UTC.

Topic guide Translation Internationalization: in template code

… within the translation block. Examples: You can use multiple expressions inside a single blocktranslate tag: NOTE: The previous more verbose format is still supported: > {% blocktranslate with book|title as book_t and author|title as author_t %} Other block tags (for …

Message files

Topic guide Translation Localization: how to create language files

NOTE: If you’re using Windows and need to install the GNU gettext utilities so > makemessages works, see gettext on Windows for more > information.

Making requests

Topic guide Testing tools The test client

Use the django.test.Client class to make requests. classClient(enforce_csrf_checks=False,raise_request_exception=True,json_encoder=DjangoJSONEncoder,*,headers=None,query_params=None,**defaults)[source] : A testing HTTP client. Takes several arguments that can customize behavior.

Making requests

Topic guide Testing tools The test client

After you call this method, the test client will have all the cookies and session data required to pass any login-based tests that may form part of a view.

TestCase

Topic guide Testing tools Provided test case classes

classTestCase[source] This is the most common class to use for writing tests in Django. It inherits from TransactionTestCase (and by extension SimpleTestCase). If your Django application doesn’t use a database, use SimpleTestCase.

LiveServerTestCase

Topic guide Testing tools Provided test case classes

… time. In particular, this means that in some cases > (for example, just after clicking a link or submitting a form), you might > need to check that a response is received by Selenium and that the next > page …

Overriding settings

Topic guide Testing tools Test cases features

… | | --- | --- | | USE_TZ, TIME_ZONE | Databases timezone | | TEMPLATES | Template engines | | FORM_RENDERER | Default renderer | | SERIALIZATION_MODULES | Serializers cache | | LOCALE_PATHS, LANGUAGE_CODE | Default translation and loaded translations …

Assertions

Topic guide Testing tools Test cases features

SimpleTestCase.assertWarnsMessage(expected_warning,expected_message,callable,*args,**kwargs)[source] SimpleTestCase.assertWarnsMessage(expected_warning, expected_message) : Analogous to SimpleTestCase.assertRaisesMessage() but for assertWarnsRegex() instead of assertRaisesRegex(). SimpleTestCase.assertFieldOutput(fieldclass,valid,invalid,field_args=None,field_kwargs=None,empty_value='')[source] : Asserts that a form field behaves correctly with various inputs. SimpleTestCase.assertFormError(form,field,errors,msg_prefix='')[source] : Asserts that a field on a form raises the provided list of errors.

T

Index

… cut - date - default - default_if_none - dictsort - dictsortreversed - divisibleby - escape - escapejs - escapeseq - filesizeformat - first - floatformat - force_escape - get_digit - intcomma - intword - iriencode - join - json_script - …

Core field arguments

Reference Form fields

Each Field class constructor takes at least these arguments. Some Field classes take additional, field-specific arguments, but the following should always be accepted:

widget

Reference Form fields Core field arguments

Field.widget The widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information.

template_name

Reference Form fields Core field arguments

By default this value is set to "django/forms/field.html". Can be changed per field by overriding this attribute or more generally by overriding the default template, see also Overriding built-in field templates.

DateField

Reference Form fields Built-in Field classes

- Validates that the given value is either a datetime.date, datetime.datetime or string formatted in a particular date format. - Error message keys: required, invalid