R
R
Razilator2020-04-26 21:33:10
Django
Razilator, 2020-04-26 21:33:10

How to create multilevel URLs based on model properties?

All the best, I would like to ask how to make multi-level URLs according to the model.
For example, there is an Article model (articles), it has a ForeignKey binding to a Category, both models have a url. There is also a game property, there is also a binding to the model, but through manytomany Game, it also has a url.
I made an absolute reference to the Article model in the model like this:

def get_absolute_url(self):
        games = self.games.all()[0]
        return reverse('article_detail_link', kwargs={'category': self.category.url, 'games': games.url, 'slug': self.url})

in urls.py output like this:
path('articles/game/<category>/<slug>/', views.ArticleDetail.as_view(), name="article_detail_link"),

Everything works, but the problem is that the same news is available in another category, by entering the link bannerlord/guides/publ_name, I will get the same thing for the next request kenshi/guides/pub_name, also bannerlord/blog/publ_name.
That is, the links are canonical.

I would also like to know how to auto-complete the url property (CharField) from the title model property, converting it to a string with small letters and hyphens instead of spaces for the url.

I read the django 3 documentation, but I didn’t really understand the urls, because the example shown was not quite the best (

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question