1

My urlconf:

urlpatterns = patterns('',
    url(r'^$', 'ping.views.services', name='services'),
    url(r'^ajax/status/(?P<id>[-\d]+)', 'ping.views.ajx_status', name='ajx_status'),
    url(r'^ajax/status/(?P<type>[-\w]+)/(?P<id>[-\d]+)(?:\?callback=(?P<callback>[-\w]+)&[_=0-9]+)?$', 'ping.views.ajx_status', name='ajx_status'),
)

Link was: http://127.0.0.1:8000/ajax/status/jsonp/1?callback=jQuery110208076630807481706_1386460596798&_=1386460596799

Output:

def ajx_status(request, id, type,callback):
    print id,type,callback

log:

1 jsonp None
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:903: RuntimeWarning: DateTimeField Ping.timestamp received a naive datetime (2013-12-08 18:12:33.831348) while time zone support is active.
  RuntimeWarning)

[08/Dec/2013 18:12:34] "GET /ajax/status/jsonp/1?callback=jQuery110208076630807481706_1386460596798&_=1386460596799 HTTP/1.1" 200 433

Text in "raw" python, works:

import re

regex = re.compile(r"^/ajax/status/(?P<type>[-\w]+)/(?P<id>[-\d]+)(?:\?callback=(?P<callback>[-\w]+)&[_=0-9]+)?")

r = regex.search("/ajax/status/jsonp/1?callback=jQuery110208076630807481706_1386460596798&_=1386460596799")

r.groupdict()

{'callback': 'jQuery110208076630807481706_1386460596798', 'type': 'jsonp', 'id': '1'}

Googled a few hours about this curious, that Django dosen't match the url on the same way how in python. Any ideas how it does not work and callback is always None ?

2 Answers 2

2

Only the path part of the URL is matched against the urlpattern. The query string is available in the view thru request.GET

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

Comments

0

In the Django url-config, there is no need to match the Query String in the url. You can query the query string data like request.GET.get('query_string_key').

For example:

request.GET.get("page")

In the urls.py you can write the following:

urlpatterns = patterns('',
    url(r'^ajax/status/(?P<type>[-\w]+)/(?P<id>[-\d]+)/$', 'ping.views.ajx_status', name='ajx_status'),
)

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.