Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urls and unicode tags #799

Open
adrien-delhorme opened this issue Mar 21, 2024 · 2 comments
Open

Urls and unicode tags #799

adrien-delhorme opened this issue Mar 21, 2024 · 2 comments

Comments

@adrien-delhorme
Copy link
Contributor

adrien-delhorme commented Mar 21, 2024

Description

URL for tags list view uses slug as path converter, which only accepts ASCII characters.:

path("tag/<slug:tag>/", TaggedListView.as_view(), name="posts-tagged"),

But django-taggit allows unicode in tags' slugs.

When a reverse() is done on this URL, it throws an exception:

Exception Type: NoReverseMatch
Exception Value: Reverse for 'posts-tagged' with keyword arguments '{'tag': 'en-été'}' not found. 1 pattern(s) tried: ['fr/blog/tag/(?P<tag>[-a-zA-Z0-9_]+)/\\Z']

Steps to reproduce

  1. Create a tag with a label containing unicode chars (for example : "En été")
  2. Assign this tag to a Post
  3. Add the BlogTagsPlugin on a page

Versions

Python == 3.11
Django == 3.2.25
djangocms-blog == 2.0.7

@adrien-delhorme
Copy link
Contributor Author

A workaround is to set TAGGIT_STRIP_UNICODE_WHEN_SLUGIFYING = True in your settings.

Tags created via a Post will have a slug without unicode characters. But tags created via Taggit section of django admin will have their slug generated by prepopulated_fields and therefore unicode characters in it... (but it is a django-taggit issue).

@adrien-delhorme
Copy link
Contributor Author

And to fix tag creation via django's admin, we can use a pre_save signal to remove unicode chars from slugs before saving a Tag instance.

from django.utils.text import slugify
from django.db.models.signals import pre_save

class YourAppConfig(AppConfig):
    ...

    def ready(self):
        from taggit.models import Tag
        pre_save.connect(slug_without_unicode, sender=Tag)

def slug_without_unicode(sender, instance, **kwargs):
  if instance.slug is not None:
      instance.slug = slugify(instance.slug, allow_unicode=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant