1

I am attempting to define a search url using Django with the following structure

www.example.com/afdata/search?device=television&category=news&query=channel&limit=30/

My goal is to extract the values television, news, channel and 30 from the above url and pass it to a view defined as follows

def search(request, device='all', category='single', query='', limit=30):
    return HttpResponse("device=%s, category=%s, query=%s, limit=%d", device, category, query, limit)

afdata is the app and in the url configuration file I defined the url as follows:

from django.conf.urls.defaults import *

urlpatterns = patterns('afdata.views',
    (r'^$', 'index'),
    (r'^search?device=(?P<device>.*)&category=(?P<category>.*)&query=(?P<query>.*)&limit=(?P<limit>d+)/$', 'search')
)

When I run using the above search query in the browser, I get 500 Internal Server Error. index responds fine. Any suggestions on what I may be doing wrong?

4 Answers 4

5

i suggest that you change your search url to:

(r'^search', 'search')

then in your view do

def search(request):
    device = request.GET.get('device', 'all')
    category = request.GET.get('category', 'single')
    query = request.GET.get('query', '')
    limit = request.GET.get('limit', 30)

   return HttpResponse("device=%s, category=%s, query=%s, limit=%d", device, category, query, limit)

This is much more robust and changeable and flexible.

Sign up to request clarification or add additional context in comments.

3 Comments

@James, would the url be structured as follows? www.example.com/search?device=television?category=news?query=channel?limit=30
@david it would be www.example.com/search?device=television&category=news&query=channel&limit=30 as you can't have ? in a url except to separate the url from the parameters
en.wikipedia.org/wiki/Query_string#Structure for more information about query strings/url
3

Django's URL patterns don't match the query part of the URL (the part starting with the ?). You only do what you're trying to do if you have parameters embedded in the URL structure itself.

See the Django docs for the exact details.

So if your URL were:

www.example.com/afdata/search/device/television/category/news/query/channel/limit/30/

Then you would match it with an URL pattern like:

urlpatterns = patterns('afdata.views',
    (r'^$', 'index'),
    (r'^search/device/(?P<device>.*)/category/(?P<category>.*)/query/(?P<query>.*)/limit/(?P<limit>d+)/$', 'search')
)

(This would be a somewhat odd URL convention, but it would work.)

If your URLs have queries in them, you match just the part before the ? and then your view needs to parse out the parameters from request.GET. (If you already have a function like that in your example, it's easy enough to write some glue code to pass the arguments.)

Comments

1

Look in your Apache logs for the stacktrace. also: you sure you don't mean HTTPResponse not HTTPRequest on the 2nd line of your search()?

1 Comment

Yes, HttpResponse is correct. I tried it but still the same problem. I will look at the stack trace.
1

By using .* Django might be grabbing up through your &category. Even if this is not the case, it is usually good practice to structure a regex in a way that disallows this. I would consider changing your .* to [^&]*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.