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.0.tar.gz (6.5 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.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_chutney-0.2.0.tar.gz
  • Upload date:
  • Size: 6.5 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.0.tar.gz
Algorithm Hash digest
SHA256 23292e9e79469767f16f7510484366b424525f34f9924345cbad157aba54b48f
MD5 4b48af1adee1991e6f8d26b5f8050243
BLAKE2b-256 6698f1a7b2f8eb03a46412144861d871c31bfde7b1f29454e855fdfd2eeaebb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_chutney-0.2.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: django_chutney-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f7ee181dc89340f01eb529e1eeb9d9524251d64a633ec7ef49e0ba8e7bc7508
MD5 6aee5dca68304edfeb1115b24f1b286c
BLAKE2b-256 895294d20415fb516336a996faa5eb5e54a46430b1061cc121470af44d030b9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_chutney-0.2.0-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