Sachin
Sachin

Reputation: 3782

regular expression in django for urls

What I am trying to do is as follows

I have urls like this /blog/1/sdc/?c=119 or /forum/83/ksnd/?c=100 What I want to do is to redirect these to a view, so that I can change the url to /blog/1/sdc/#c119

One way would be to do this is to make provision in views of each of the app, where such a url maybe generated, but that is not scalable. What I want to do is to catch any url that has ?c=<some_digit> at the end and redirect to my custom view.

Can anybody help, I am not good with regex.

Upvotes: 0

Views: 182

Answers (1)

Greg
Greg

Reputation: 10352

You can't do this in your urlconf, it doesn't match anything in the query string. What you'll need to do is write a middleware along the lines of this:

class RedirectMiddleware:
    def process_request(self, request):
        if 'c' in request.GET:
            # return a HttpResponseRedirect here

See https://docs.djangoproject.com/en/dev/topics/http/middleware/ for more details.

Upvotes: 4

Related Questions