Skip to main content

Tasty accompaniments to go with the Django web framework.

Project description

Django Chutney

Tasty accompaniments to go with the Django web framework

My personal collection of miscellaneous useful bits of code for use in Django projects.

Installation

pip install django-chutney

Usage

The library is intended to be a pick and mix bucket from which you can choose to use whichever bits you want.

Test case helpers

These require BeautifulSoup to be installed:

pip install beautifulsoup4>=4

FormHelper

This provides functionality to allow you to test HTML forms in a way that's more accurate than the standard approach with django.test.Client.

A common approach to testing form submissions to Django views is to submit a dict of data to the view, but simply doing that doesn't check that the HTML form you're rendering actually allows those values to be submitted. For example, your test case might submit a value for a field which isn't an option in the rendered <select>. This approach also means that you may not be submitting values which would be automatically submitted by a web browser, such as hidden fields.

django_chutney.test_utils.FormHelper provides a way for a test case to to submit a form while checking that the values you're submitting are actually value that the browser would allow a user to submit. It also automatically submits the values of any hidden inputs the form.

from django.test import TestCase
from django_chutney.test_utils import FormHelper

class MyFormTestCase(TestCase, FormHelper):

    def test_form_page(self):
        page_response = self.client.get(reverse("my_view"))
        # Submit a form to the page, with automatic checking that the values you're submitting are
        # possible values for a browser to submit based on the HTML of the form in the page.
        # Also submits hidden values that are in the form, even if you don't specific them.
        data = {"field1": "some-value"}
        post_response = self.submit_form(response, "#my-form", data)
        self.assertEqual(post_response.status_code, 302)

SoupHelper

This class provides additional helper methods for testing pages using BeautifulSoup.

assert_link_in_soup(url: str, soup: bs4.BeautifulSoup)

Tests that the given URL appears in a link (<a/> tag) in the given soup, while being order-insensitive about the order of query params.

assert_text_in_soup(text, soup, tag: str | None = None, exact=False)

Tests that the given text appears in the soup, optionally inside the given type of tag. If exact is True then the tag must contain only the given text.

find_element_containing_text(soup, text, tax=None, exact=False) -> bs4.BeautifulSoup | None

Similar to assert_text_in_soup, but returns the first found element which matches.

from django.test import TestCase
from django_chutney.test_utils import SoupHelper

class MyTestCase(TestCase, SoupHelper):

    def test_page(self):
        response = self.client.get(reverse("my_view"))
        soup = self.soup(response)
        # Test that a link to a specific URL exists, with order-insensitive query params
        self.assert_link_in_soup("https://www.thing.com/?unordered=query&params=value", soup)
        self.assert_text_in_soup("Hello", soup)
        table_row = self.find_element_containing_text("John Smith")
        # The table row should contain a link to the user's profile
        self.assert_link_in_soup("/user/profile/{john_smith.pk}/", table_row)

Template tags & filters

get filter

Allows you to use a variable in the template as a key to extra an item from a dict to to extract an attribute value from an object.

In Django templates, doing {{obj.my_key}} will try to do obj["my_key"]. So you can't use a variable to supply the key.

This filter allows you to do {{obj|get:my_var}}, which will do obj[my_var] instead.

{% load chutney_tags %}
<p>
    {{ my_dict|get:my_var }}
</p>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_chutney-0.2.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_chutney-0.2.3-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file django_chutney-0.2.3.tar.gz.

File metadata

  • Download URL: django_chutney-0.2.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_chutney-0.2.3.tar.gz
Algorithm Hash digest
SHA256 c78fe0e75f622ad01a42aa89cb207cfcecf11884ef6073caf593f8368657f3aa
MD5 6aa126f1869086cc3df85614cc2bb6bd
BLAKE2b-256 9aeba60b56bf68a0422f81a7a0ef34ff4875c572e8ff8e91bb5c627a37ef9456

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_chutney-0.2.3.tar.gz:

Publisher: publish.yml on adamalton/django-chutney

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_chutney-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: django_chutney-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_chutney-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7c01bd6662b95505b43da81712e040c1670f58f3ab257b158a86810cebc8da87
MD5 f926f1313e9dcad36e159781cce09ba6
BLAKE2b-256 68b1e88c0e92eb2b843a4d285e53f286d70a3c0641b2ccef7f7a62e2389aba4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_chutney-0.2.3-py3-none-any.whl:

Publisher: publish.yml on adamalton/django-chutney

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page