Django Mailbox URL Error

I am trying to do REST Api in Django by outputting Json. I am having problems if I make a POST request using curl in a terminal. The error I get is this

You call this URL via POST, but the URL does not end with a slash and you have APPEND_SLASH set. Django cannot redirect slash URLs while saving POST data. Change the form to point to 127.0.0.1:8000/add/( pay attention to the trailing slash) or set APPEND_SLASH = False in the Django settings.

My url.py

from django.conf.urls.defaults import patterns, include, url import search # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', url(r'^query/$', 'search.views.query'), url(r'^add/$','search.views.add'), ) 

and my views

 # Create your views here. from django.http import HttpResponse from django.template import Context,loader import memcache import json def query(request): data=['a','b'] mc=memcache.Client(['127.0.0.1:11221'],debug=0) mc.set("d",data); val=mc.get("d") return HttpResponse("MEMCACHE: %s<br/>ORIGINAL: %s" % (json.dumps(val),json.dumps(data)) ) def add(request): #s="" #for data in request.POST: # s="%s,%s" % (s,data) s=request.POST['b'] return HttpResponse("%s" % s) 

I know that it does not give Json, but I have the problem mentioned above when I make a POST request in terminal

 curl http://127.0.0.1:8000/add/ -db=2 >> output.html 

I am new to django.

+12
python django restful-architecture
source share
2 answers

First, make sure that you submit the request at http://127.0.0.1/add/ not at http://127.0.0.1/add .

Secondly, you can also free the view from csrf processing by adding the @csrf_exempt decorator - since you are not sending the corresponding token from cURL.

+23
source share

For URL consistency, Django has a parameter named APPEND_SLASH that always appends a slash to the end of the URL if it was not sent this way. This ensures that /my/awesome/url/ always served from this URL instead of the two /my/awesome/url and /my/awesome/url/ .

However, Django does this by automatically redirecting the version without a slash at the end with a slash at the end. Redirects do not carry the status of the request, so when this happens, your POST data will be deleted.

All you have to do is make sure that when you send your POST you will send it to the version with a slash at the end.

+18
source share

All Articles