Question

I've created a very simple blog, but have been running into a couple URL issues. For my tag & specific post views I run into the following issues.

Specific Post View Example
These two sites render the same, and would like the second one to render a 404.
website.com/post/1/hello-world
website.com/post/1/hello-world/non-sense (should render 404)

Tag View
website.com/tag/python: this will render all posts tagged python, great. However...
website.com/tag/python/party: this will render all posts tagged "python/party" instead of rendering a 404.

Here is my URL patterns setup so you can take a look.

url(r'^post/(?P<pk>\d+)/(?P<post_title>)', DetailView.as_view(
                        model = post,
                        template_name = "post.html")),
url(r'^post/$', ListView.as_view(
                        queryset = post.objects.all().order_by("-date"),
                        template_name = "archives.html")),
url(r'^archives/$', ListView.as_view(
                        queryset = post.objects.all().order_by("-date"),
                        template_name = "archives.html")),
url(r'^tag/(?P<tag>[\w|\W]+)', 'tags'),

Updated
Solution for tag:

url(r'^tag/(?P<tag>[\w]+)\W*/$', 'tags'),

Solution for post:

url(r'^post/(?P<pk>\d+)/(?P<post_url>[\w-]+)/$', DetailView.as_view(
                        model = post,
                        template_name = "post.html")),

Thank you Huckleberry Finn and krakax for all the help!

Was it helpful?

Solution

Your post URLconf regex

url(r'^post/(?P<pk>\d+)/(?P<post_title>)', DetailView.as_view(
                    model = post,
                    template_name = "post.html")),

Should changed to

url(r'^post/(?P<pk>\d+)/(?P<post_title>[-\w]+)/$', DetailView.as_view(
                    model = post,
                    template_name = "post.html")),

means URLconf is ending with end-slash

Anyway, Try to define you DetailView URLconf after post ListView. In my opinion if you change your list view and detailview to posts/ and post/ you problem will be solved. The solution is same for tags URLconf issue.

OTHER TIPS

Your regex

r'^tag/(?P<tag>[\w|\W]+)'

means that group tag will contain all caracters after 'tag/'. [\w|\W] means 'all alphanumerics' or 'all but alphanumerics'. This is equivalent to 'all caracters'.

It should be changed to

r'^tag/(?P<tag>[\w]+)\W*'

This will stop your group at first non alphanumeric

Sure, right now I see 2 ways.

First, change to:

r'^tag/(?P<tag>\w+)(?P<end>\W.*)'

(BTW, [] is not necessary in your case) This way, you get the group called 'end' as a parameter of your tag controller and you can test it and redirect to your 404 custom page or generate a 404 classic error.

Second possibility is: Add another url hook and controller:

url(r'^tag/(?P<tag>\w+)\W*', 'tags404'),

url(r'^tag/(?P<tag>\w+)$', 'tags'),

This way, you forbid any url that contains others caracters than alphanumerics after the 'tag/'.

If you want to allow just one optional '/' at the end, you can write in place of your new hook:

url(r'^tag/(?P<tag>\w+)/?$', 'tags'),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top