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.1.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.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_chutney-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 321d591a0957c0ec4470c2f31a8e904ba7b35bcc70687b0f6831704a00ae7606
MD5 d91f9d98916dda126c8024769473caa9
BLAKE2b-256 a81424144968c5a305d6a87e82e81165ee8cd4a7c8af10429b8a5821db2514c7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_chutney-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 91772b644665f62c07fa878283089fc4d7febbb8bbd6d4d61c3a40bb6542fe77
MD5 9268c29ae557cb2d857ad3da0ce8e698
BLAKE2b-256 c06b26be79fef4dca68750d8e86ce804e858e5ed92746ac4580b00c0e8f43804

See more details on using hashes here.

Provenance

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