Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

python - Django POST URL error

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, make sure that you send the request to http://127.0.0.1/add/ not http://127.0.0.1/add.

Secondly, you may also want to exempt the view from csrf processing by adding the @csrf_exempt decorator - since you aren't sending the appropriate token from cURL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...