Hi I have a url pattern like this:
url(r'^post/(?P<post_id>\d+)/(?P<post_slug>[\w|\W]+)/$', views.detail, name="detail"),
But I dont use post_slug in my view. There is no problem when someone enters the url like this:
127.0.0.1:8000/post/5/my-first-post/
There is also no problem when someone enters the url like this:
127.0.0.1:8000/post/5/mydflgkdfgld/
How can I handle it? I want both id(for quering) and slug(for beatiful urls).
Thank you for your comments.First state of my view is like this:
def detail(request, post_id, post_slug):
post = get_object_or_404(Post, is_pub=True, pk=post_id)
return render(request, 'blog/index.html', {'post': post})
But I want to make my query with only post_id. But in my new view I used post_slug like this:
def detail(request, post_id, post_slug):
post = get_object_or_404(Post, is_pub=True, pk=post_id)
if post.slug == post_slug:
return render(request, 'blog/index.html', {'post': post})
else:
return HttpResponseRedirect(reverse('blog:detail', args=[post_id, post.slug]))
But I dont know is it ok or not?