-
Notifications
You must be signed in to change notification settings - Fork 9
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
Dealing with titles that are too long for a slug field #35
Comments
I've run into this too. I'd be happy to submit a PR if you'll let me know what sort of slug-ification scheme you'd like to see. Here are some ideas: Idea 1. Just keep the first 50 chars: >>> title = "Hi, I'm just your average every day title that's too long for it's own good"
>>> slugify(title)[:50]
'hi-im-just-your-average-every-day-title-thats-too-' Idea 2. Prefix (or postfix) the slug with the Entry's id. This'll prevent issues if you had similar titles whose slugs collide. I've done this in other projects, but it takes two queries to save an Entry initially. >>> entry_id = str(entry.id)
>>> "-".join([entry_id, slugify(title)[:49 - len(entry_id)]])
'123-hi-im-just-your-average-every-day-title-thats-' Idea 3. Similar to idea 2, we could generate a uuid or (a any hash) and use a short slice of it as a prefix or postfix for the slug. >>> from uuid import uuid4
>>> '-'.join([slugify(title)[:43], str(uuid4())[:6]])
'hi-im-just-your-average-every-day-title-tha-38a9bb' Just a few ideas. Let me know what you think, and I'll send you a PR. |
One other thought: Idea 2 has the added benefit that you can look up an entry by it's ID (without the rest of the slug) if you wanted to. This also means that changing a title might not break an old link to an entry. Perhaps that's Beyond the Scope, tho :) |
Great! I think the reason the original implementer did a split/cut/recombine was to avoid any interrupted words or trailing dashes. I wonder about combining the fork's approach with idea 1 where the code would start with more parts in-tact and work it's way down until it's within the character constraints. I don't think performance is a big problem because it is only done on save. Good secondary point about idea 2. Looking up by Id instead is actually mentioned in #27. Though I like the URL scheme there better. So I guess I would suggest a modified form of idea 1 and tackling a form of idea 2 separately (by the resolution to #27) |
The tests would land in the same spot regardless. |
Ensure entry slugs fit in a slug field. Fixes #35
Came across this issue in one of the forks. https://github.com/fedejaure/django-andablog/commit/1ece7f427f2bf215e67a1ed23fd81b7783b02238
A similar approach but something that makes the parts used a bit more dynamic (rather than always the first 4) could work. Tests for this would best be placed in andablog's test_models.
The text was updated successfully, but these errors were encountered: