An improved URL selector to choose between internal models and external URLs
Project description
django-any-urlfield
The any_urlfield module provides an improved URL selector that supports both URLs to internal models and external URLs.
This addresses is a common challenge in CMS interfaces; where providing a URLField makes it hard to enter internal URLs, while providing a ModelChoiceField makes it too inflexible.
Relevant public classes:
Model fields:
AnyUrlField: allow users to choose either a model or external link as URL value
Form widget rendering:
HorizonatalRadioFieldRenderer
SimpleRawIdWidget
For more details, see the documentation at Read The Docs.
Screenshot
Installation
First install the module, preferably in a virtual environment. It can be installed from PyPI:
pip install django-any-urlfield
Or the current folder can be installed:
pip install .
Configuration
Add the module to the installed apps:
INSTALLED_APPS += ( 'any_urlfield', )
Usage
In a Django model, the field can be included:
from django.db import models from any_urlfield.models.fields import AnyUrlField class MyModel(models.Model): title = models.CharField("Title", max_length=200) url = AnyUrlField("URL")
By default, the AnyUrlField only supports linking to external pages. To add support for your own models (e.g. an Article model), include the following code in models.py:
from any_urlfield.models import AnyUrlField AnyUrlField.register_model(Article)
Now, the AnyUrlField offers users a dropdown field to directly select an article. By default, it uses a django.forms.models.ModelChoiceField field with a django.forms.widgets.Select widget to render the field. This can be customized using the form_field and widget parameters:
from any_urlfield.models import AnyUrlField from any_urlfield.forms.widgets import SimpleRawIdWidget AnyUrlField.register_model(Article, widget=SimpleRawIdWidget(Article))
Now, the Article model will be displayed as raw input field with a browse button.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-any-urlfield-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b838d583e1b7374d42f612b2be537e96bddc0f78ef23667c1a7662f9bcb3da96 |
|
MD5 | 9632973428a7d64c7276c9e779e0baa9 |
|
BLAKE2b-256 | d0d8d90643e2139cb7e6595d100d8a5286f2379e1d2d64d0ba79c2d85137a89c |