DJango POST URL错误 [英] DJango POST URL error
问题描述
您通过POST调用此URL,但该URL不会以斜杠结尾,而
设置了APPEND_SLASH。 Django无法重定向到斜杠URL
同时维护POST数据。更改您的表单指向
127.0.0.1:8000/add/(注意尾部斜杠),或在您的Django设置中设置APPEND_SLASH = False。
我的url.py是
从django.conf.urls.defaults导入模式,包括, url
import search
#取消注释下面两行以启用admin:
#from django.contrib import admin
#admin.autodiscover()
urlpatterns = patterns('',
url(r'^ query / $','search.views.query'),
url(r'^ add / $','search.views.add'),
)
#在这里创建您的观点。
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,数据);
val = mc.get(d)
返回HttpResponse(MEMCACHE:%s< br /> ORIGINAL:%s%(json.dumps val),json.dumps(data)))
def add(request):
#s =
#for request.POST中的数据:
#s =%s,%s%(s,data)
s = request.POST ['b']
返回HttpResponse(%s%s)
我知道它不给Json,但是当我在终端
$ b中进行POST请求时,我遇到上述问题$ b
curl http://127.0.0.1:8000/add/ -db = 2>> output.html
我刚刚到django。
首先,请确保您将请求发送到 http://127.0.0.1/add/
而不是code> http://127.0.0.1/add 。
其次,您可能还希望将视图从csrf处理中免除添加 @csrf_exempt
装饰器 - 因为您没有从cURL发送适当的令牌。
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.
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.
这篇关于DJango POST URL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!