0

If I wanted to send the blogpost id to the url pattern, and have the url automatically turn into something like www.blog.com/post/2/this-is-my-second-blogpost, how might I do that?

Inside of urls.py I have a url pattern that accepts the blogpost id, and its title. It doesn't seem to be working, and once it does, would be tedious to add the title for every page in this manner.

urls.py:path('post/<int:post_id>/<str:post_title>', views.view_post, name='view_post'),

blogposts.html template: <a href="{% url 'blogs:view_post' post.id post.title%}">Read More</a>

views.py: def view_post(request, post_id, post_title):.

pyknight202
  • 502
  • 1
  • 3
  • 13

2 Answers2

2

Simply, add the default argument of post_title to be None. If you are wishing to get the post_title directly from the post_id, neclect the post_title, doing this won't give you error as functions already got all the satisfied argument values.

So:

def view_post(request, post_id, post_title=None):

Edit

Okay, what user wanted was about slug field. Slug field is the field type in django model. Basically, it is used for the url type. In above problem, what pyknight202 wanted was the url patter for the post_title. So, you could not use tilte directly as url as they contain spaces, so for that you have to add post_title in hyphens or underscore to be readable. Therefore, you need slug field in Post model.

https://helloworld.com/1/this-is-example

So, 1 is post_id as slug could not be unique, so you could not only retrieve from post_title therefore you need post_id too. Refs

Community
  • 1
  • 1
Biplove Lamichhane
  • 3,337
  • 3
  • 11
  • 28
  • would it be possible to send only the id and have the url use that to add the post_title to the end? – pyknight202 May 07 '20 at 15:39
  • a template sends the user and post_id to a new url pattern, which by using the id to get the post object, concatenates the post_title to the end of the url – pyknight202 May 07 '20 at 15:47
  • Yes that's possible absolutely.... If you can retrieve ```post_title``` from ```post_id```, then you don't have to add that to parameter... – Biplove Lamichhane May 07 '20 at 15:48
  • could you explain how I could do that without sending post_title to the view? my attempts have returned ```view_post() got an unexpected keyword argument 'post_title'```. It seems post_title=None would be changed once post_title is sent to the view. – pyknight202 May 07 '20 at 16:11
  • well, i missunderstood what you said before... you want to add title url at the end, that's possible but you have to redirect it to another similar url..which i think is not good solution. – Biplove Lamichhane May 07 '20 at 16:43
  • I found a solution different from the one we were proposing, however it was deleted by a moderator. I think it would be beneficial to those that come across this problem to see a solution. Slugs are a field type for models, one created for this purpose. https://docs.djangoproject.com/en/3.0/topics/http/urls/ – pyknight202 May 08 '20 at 21:40
  • I have edited from your comment, so let me know if it is what you wanted. !!! Thank you ... – Biplove Lamichhane May 09 '20 at 04:13
1

Maybe you should use a slug field for the url instead of the combination of id and title, your url will look like example.org/title-to-object-2/. Now you have a unique slug field with the title and the id.

Shanks
  • 613
  • 5
  • 20