Giter Site home page Giter Site logo

marcanuy / django-slugs-example-app Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 5.0 170 KB

A basic app to show how to add slugs to models

Home Page: https://simpleit.rocks/python/django/generating-slugs-automatically-in-django-easy-solid-approaches/

License: MIT License

Python 81.59% HTML 17.25% Makefile 0.73% Procfile 0.42%
django slugify url seo django-application

django-slugs-example-app's Introduction

django-slugs-example-app

A basic app to show how to generate slugs based in a model's field.

This app shows the concepts explained at: https://simpleit.rocks/python/django/generating-slugs-automatically-in-django-easy-solid-approaches/

Demo: https://django-slugs-example-app.herokuapp.com/ (db reset daily)

Installation

python3 -m venv ~/.virtualenvs/django-slugs-example-app
source ~/.virtualenvs/django-slugs-example-app/bin/activate
pip install -r requirements.txt
./manage.py migrate

Run example site: ./manage.py runserver.

Models

models

URLs

/	blog.views.ComparatorListView	article-list
/blog/<int:pk>-<str:slug>/	blog.views.ArticlePkAndSlugDetailView	article-pk-slug-detail
/blog/<str:slug>	blog.views.ArticleUniqueSlugDetailView	articleunique-slug
/blog/create	blog.views.ComparatorFormView	article-create

django-slugs-example-app's People

Contributors

dependabot[bot] avatar marcanuy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-slugs-example-app's Issues

Possible bug if title length is greater than max_length

If self.title is 99 characters or longer then, if it's necessary to append "-i", won't this cause the slug to be longer than SlugField max length of 100? Also max_length is defined but not used

class ArticleUniqueSlug(Article):

    slug = models.SlugField(default="", editable=False, max_length=100)  # random length

    def get_absolute_url(self):
        kwargs = {"slug": self.slug}
        return reverse("articleunique-slug", kwargs=kwargs)

    def _generate_slug(self):
        max_length = self._meta.get_field("slug").max_length
        value = self.title
        slug_candidate = slug_original = slugify(value, allow_unicode=True)
        for i in itertools.count(1):
            if not ArticleUniqueSlug.objects.filter(slug=slug_candidate).exists():
                break
            slug_candidate = "{}-{}".format(slug_original, i)

        self.slug = slug_candidate

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.