Skip to main content

Django global query strings

Python 3 Django 3 Python CI codecov Code style: black

Django global query strings allows adding query strings (query params) globally.
The app looks for all the <a> tags in a html content thanks to BeautifulSoup.

There are two ways of using the app. Each way can be used separately or conjointly:

  • Using the middleware GlobalQueryStringsMiddleware
  • Using the utility function add_query_strings_to_links

Installation

Install with pip:

pip install django-global-query-strings

Setup

In order to make django-global-query-strings works, you'll need to follow the steps below:

Settings

First, you need to add the app and the middleware to your settings. The middleware is optional depending on your usage:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',

    'global_query_strings',
    ...
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    
    'global_query_strings.middleware.GlobalQueryStringsMiddleware',
    ...
)

Settings

Here is the list of all the settings with their default values:

GLOBAL_QUERY_STRINGS_IGNORE_URLS = []
GLOBAL_QUERY_STRINGS_IGNORE_RELATIVE_PATHS = False
GLOBAL_QUERY_STRINGS_PARAMS = {}
GLOBAL_QUERY_STRINGS_EXCLUDE_PATHS_LIST = []

GLOBAL_QUERY_STRINGS_IGNORE_URLS

GLOBAL_QUERY_STRINGS_IGNORE_URLS takes a list of domain name or urls e.g.: ["example.com", "https://www.foo.org"]. When the html parser finds those values, the global query strings won't be added to the urls.

GLOBAL_QUERY_STRINGS_IGNORE_RELATIVE_PATHS

GLOBAL_QUERY_STRINGS_IGNORE_RELATIVE_PATHS is a boolean. When set to True, the parser will ignore relative paths.

GLOBAL_QUERY_STRINGS_PARAMS

GLOBAL_QUERY_STRINGS_PARAMS takes a dictionary of global query strings to set e.g.: {"foo": "bar", "lorem": "ipsum"}. The query strings found in GLOBAL_QUERY_STRINGS_PARAMS would be replaced / added to the urls found by the parser.

GLOBAL_QUERY_STRINGS_EXCLUDE_PATHS_LIST

GLOBAL_QUERY_STRINGS_EXCLUDE_PATHS_LIST is a list of paths to exclude that takes place in the middleware only. All the paths that starts with any elements of the list will bypass the middleware. e.g.: ["/admin"]

Usage

Here is an example of input / output with the following settings:

GLOBAL_QUERY_STRINGS_IGNORE_URLS = ["example.com", "https://www.foo.org"]
GLOBAL_QUERY_STRINGS_IGNORE_RELATIVE_PATHS = True
GLOBAL_QUERY_STRINGS_PARAMS = {"foo": "bar", "lorem": "ipsum"}

Input:

<html lang="en">
    <body>
        <a href="https://www.foo.org">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry.
        <a href="https://foo.org">Lorem Ipsum</a> has been the industry's standard dummy text ever since the 1500s,
        when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only <a href="https://www.bar.com">five centuries</a>, but also the leap into electronic
        typesetting, <a href="/lorem/ipsum">remaining essentially</a> unchanged.
        It was popularised in the 1960s with the release of <a href="https://www.bar.com?example=true">Letraset sheets</a>
        containing Lorem Ipsum passages, and more recently with desktop <a href="https://www.foo.org?foo=ipsum">publishing software</a>
        like Aldus PageMaker including versions of Lorem Ipsum.
    </body>
</html>

Output:

<html lang="en">
    <body>
        <a href="https://www.foo.org">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry.
        <a href="https://foo.org?foo=bar&lorem=ipsum">Lorem Ipsum</a> has been the industry's standard dummy text ever since the 1500s,
        when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only <a href="https://www.bar.com?foo=bar&lorem=ipsum">five centuries</a>, but also the leap into electronic
        typesetting, <a href="/lorem/ipsum">remaining essentially</a> unchanged.
        It was popularised in the 1960s with the release of <a href="https://www.bar.com?example=true&foo=bar&lorem=ipsum">Letraset sheets</a>
        containing Lorem Ipsum passages, and more recently with desktop <a href="https://www.foo.org?foo=bar&lorem=ipsum">publishing software</a>
        like Aldus PageMaker including versions of Lorem Ipsum.
    </body>
</html>

Tests

Testing is managed by pytest. Required package for testing can be installed with:

pip install -r test_requirements.txt

To run testing locally:

pytest

Metadata

Release files for django-global-query-strings 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-global-query-strings 0.4.0
File Size Uploaded
django-global-query-strings-0.4.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-global-query-strings 0.4.0
File Interpreter ABI Platform
django_global_query_strings-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / django-global-query-strings-0.4.0.tar.gz

Download URL django-global-query-strings-0.4.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5a8d56fbb2bfe0e8048291754319b61b8552eef07ca1362ef70fae3337686215
BLAKE2b-256 checksum
How to use checksums
fd4a17f9d266a50fbadd50a1391bb00f7d8ba8931b6afe110413500afe97f202
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / django_global_query_strings-0.4.0-py3-none-any.whl

Download URL django_global_query_strings-0.4.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46efdda73d42f94dc2f2946188f27640173169dad3c0035d700925c0ad3e8146
BLAKE2b-256 checksum
How to use checksums
6f7850b9a212399a0a450ba02438e26ff5a7ad3f9ce95b88033f843d29ec459c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page