2

I am new to Django. I have to write a moke. My server will look at a specific address. Like this:

portal/client_api.ashx?client=SAPRA&key=1234234&func=status&code=99999

I wrote:

urls.py

from django.conf.urls import patterns, url
from rt_moke import views
urlpatterns = patterns('',
    url(r'code=(?P<code_id>\w+)/', views.Sapata, name='sapata'),
    )

and views.py

from django.http import HttpResponse
status = {u"99999": u'{"code": "99999","status": "undelivered"}',\
     u"88888": u'{"code": "88888","status": "delivered"}',\
     }
def Sapata(request, code_id):
    return HttpResponse(status[code_id])

When I request for portal/client_api.ashx?client=SAPRA&key=1234234&func=status&code=99999 without ? mark - it works, and with ?- not. I understand, that it is query string and Django skips it in the regexp. So what can I do?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Igor Korolev
  • 101
  • 2
  • 10
  • 3
    Look into `request.GET`, as mentioned in [this][1] SO question. [1]: http://stackoverflow.com/a/157295/1513010 – SunPowered Dec 19 '13 at 14:55

1 Answers1

6

This URL:

portal/client_api.ashx?client=SAPRA&key=1234234&func=status&code=99999

has two parts, the path:

portal/client_api.ashx

and the query string:

client=SAPRA&key=1234234&func=status&code=99999

which is parsed into request.GET.

In views.py you should get params from request (like simple dict in request.GET), for example:

def test(request):
    code = request.GET.get('code') # here we try to get 'code' key, if not return None
    ...

and of course, we can't use GET params to parse URLs in urls.py. Your urls.py should looks like:

from django.conf.urls import patterns, url
from rt_moke import views

urlpatterns = patterns('',
    url(r'^portal/client_api\.ashx$', views.Sapata, name='sapata'),
)

P.S. Please, don't use capital letters in names of functions.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
greg
  • 1,417
  • 9
  • 28