4

Is there any built-in or 3rd party app for custom styled urls?

I'd like to have custom urls such as: example.com/article-type/article-name-2015-24-12

Where article-type would be a created from a foreign key and at the end of the url there would be the published date.

And in templates I'd call them using {% url 'article' article.pk %}. (So in the future when my boss decides to change the url structure I don't have to change it everywhere)

Is there anything like that? If not could anyone kick me to the right direction how to implement a such feature?

1
  • Unless I am misunderstanding what you want - you can do this with just Django urls. But instead of {% url 'article' article.pk %} it would be something like {% url 'article' article.name article.date %} or better yet, have a slug on the Article model and make sure it is unique then you can just use the slug as such {% url 'article' article.slug %} Commented Apr 20, 2016 at 17:54

1 Answer 1

2

You could build the URL on the model, overriding the get_absolute_url() method.

Something like this

def Something(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField()
    created = models.DateTimeField()
    article_type = models.ForeignKey(ArticleTypes)

    def get_absolute_url(self):
        return '/{}/{}-{}-{}-{}'.format(
            self.article_type, self.slug, self.created.year, 
            self.created.month, self.created.day)

And in the template, you'd use

<a href="{{ something.get_absolute_url }}">{{ something.name }}</a>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.