I am trying to make a REST Api in Django by outputting Json. I am having problems if i make a POST request using curl in terminal. The error i get is
You called this URL via POST, but the URL doesn't end in a slash and
you have APPEND_SLASH set. Django can't redirect to the slash URL
while maintaining POST data. Change your form to point to
127.0.0.1:8000/add/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
My url.py is
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 are
# 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 its not giving Json but I'm having the problem mentioned above when i make POST request in terminal
curl http://127.0.0.1:8000/add/ -d b=2 >> output.html
I am new to django though.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…