Skip to main content

An advanced slug field with URL previews.

Project description

django-slug-preview

An advanced slug field offering live URL previews.

This is inspired by the “Permalink” preview that WordPress offers. While not looking as fancy yet, this is a good start for Django projects. Improvements are welcome!

https://github.com/edoburu/django-slug-preview/raw/master/docs/images/slugpreview1.png

Installation

First install the module, preferably in a virtual environment. It can be installed from PyPI:

pip install django-slug-preview

Or the current folder can be installed for development:

pip install -e .

Add slug_preview to your INSTALLED_APPS:

INSTALLED_APPS += (
    'slug_preview',
)

Usage

  • Use slug_preview.models.SlugPreviewField in your models instead of the standard models.SlugField.

  • Add slug_preview.forms.SlugPreviewFormMixin in your forms.

For example:

from django.db import models
from slug_preview.models import SlugPreviewField

class MyModel(models.Model):
    slug = SlugPreviewField(_("Slug"))

In the admin you can use the SlugPreviewModelForm shortcut:

from django.contrib import admin
from django import forms
from slug_preview.forms import SlugPreviewModelForm

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    form = SlugPreviewModelForm

In custom forms, use SlugPreviewFormMixin directly:

from django import forms
from slug_preview.forms import SlugPreviewFormMixin
from .models import MyModel

class MyModelForm(SlugPreviewFormMixin, forms.ModelForm):
    class Meta:
        model = MyModel

Special model URLS

When a model has a custom URL layout (not just /{slug}/), you can add a get_absolute_url_format() method in the model. For example:

from django.db import models
from slug_preview.models import SlugPreviewField

class Page(models.Model):
    parent = models.ForeignKey('self')
    slug = SlugPreviewField(_("Slug"))
    # ...


    def get_absolute_url(self):
        if self.parent_id:
            return "{0}{1}/".format(self.parent.get_absolute_url(), self.slug)
        else:
            return "/{0}/".format(self.slug)

    def get_absolute_url_format(self):
        if self.parent_id:
            return "{0}{{slug}}/".format(self.parent.get_absolute_url())
        else:
            return "/{slug}/"

For a blog, you can add the /blog/{year}/{month}/ format too:

from django.core.urlresolvers import reverse
from django.db import models
from django.utils.timezone import now
from slug_preview.models import SlugPreviewField

class Article(models.Model):
    slug = SlugPreviewField(_("Slug"))
    pubdate = models.DateTimeField(default=now)
    # ...

    def get_absolute_url(self):
        root = reverse('article_list')
        return "{root}/{year}/{month}/{slug}/".format(
            root=reverse('article_list').rstrip('/'),
            year=self.pubdate.strftime('%Y'),
            monthy=self.pubdate.strftime('%M'),
            slug=self.slug
        )

    def get_absolute_url_format(self):
        root = reverse('article_list')
        pubdate = self.pubdate or now()
        return "{root}/{year}/{month}/{{slug}}/".format(
            root=reverse('article_list').rstrip('/'),
            year=pubdate.strftime('%Y'),
            monthy=pubdate.strftime('%M'),
        )

Improving this package

This module is designed to be usable for other projects too. In case there is anything you didn’t like about it, or think it’s not flexible enough, please let us know. We’d love to improve it! Pull requests are welcome too. :-)

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-slug-preview-2.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

django_slug_preview-2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file django-slug-preview-2.0.tar.gz.

File metadata

  • Download URL: django-slug-preview-2.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for django-slug-preview-2.0.tar.gz
Algorithm Hash digest
SHA256 a85b63be851575d645428719ea297c9a68d85b6b1c70b917794ec079905c4cfc
MD5 6ec7cd5c3cfaf771e956452825d33699
BLAKE2b-256 3588f84b68b1e0782bd1dfa49d7f65eed571baf215d6aa2925f0d2af510497af

See more details on using hashes here.

File details

Details for the file django_slug_preview-2.0-py3-none-any.whl.

File metadata

  • Download URL: django_slug_preview-2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for django_slug_preview-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98690787ec4a25bfd8b85c1f5a8148e5e3d7a4fb37a8feb77ecd173dedd639ac
MD5 31946a295238d628012295c4748a3f31
BLAKE2b-256 8422f289fefc5301e38186c559c2d334243a651671b37c60900e2f859ce17845

See more details on using hashes here.

Supported by

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