我正在学习Django 1.6。
我想使用HTTP POST请求发布一些JSON,我正在使用Django来完成此任务的学习
我尝试使用request.POST['data']
,request.raw_post_data
,request.body
,但没有一个适合我
我的views.py是
import json
from django.http import StreamingHttpResponse
def main_page(request):
if request.method=='POST':
received_json_data=json.loads(request.POST['data'])
#received_json_data=json.loads(request.body)
return StreamingHttpResponse('it was post request: '+str(received_json_data))
return StreamingHttpResponse('it was GET request')
我使用requests模块发布JSON数据。
import requests
import json
url = "http://localhost:8000"
data = {'data':[{'key1':'val1'}, {'key2':'val2'}]}
headers = {'content-type': 'application/json'}
r=requests.post(url, data=json.dumps(data), headers=headers)
r.text
r.text
应该打印该消息并发布数据,但我无法解决这个简单的问题。请告诉我如何在Django 1.6中收集发布的数据?
答案 0 :(得分:92)
您在这里混淆了表单编码和JSON数据。 request.POST['foo']
用于表单编码数据。您正在发布原始JSON,因此您应该使用request.body
。
received_json_data=json.loads(request.body)
答案 1 :(得分:57)
对于python3,你必须首先解码body:
$(document).ready(function(){
$("body").on("change", "input[type='checkbox']",function() {
var that = this;
if (!$(this).parent().hasClass("remember-label")){
$(this).parent().css("background-color", function() {
return that.checked ? "#C0E9F7" : "";
});
}
});
$("body").on("click", "input[type=radio]" ,function() {
$(this).parents().eq(1).siblings().css("backgroundColor","");
$(this).parents().eq(1).css("backgroundColor","#C0E9F7");
});
});
答案 2 :(得分:-1)
一种方法是使用ajax。
前端(javascript):-
// LIB
type Arguments<T> = [T] extends [(...args: infer U) => any]
? U
: [T] extends [void] ? [] : [T]
interface TypedEventEmitter<Events> {
addListener<E extends keyof Events> (event: E, listener: Events[E]): this
on<E extends keyof Events> (event: E, listener: Events[E]): this
once<E extends keyof Events> (event: E, listener: Events[E]): this
prependListener<E extends keyof Events> (event: E, listener: Events[E]): this
prependOnceListener<E extends keyof Events> (event: E, listener: Events[E]): this
removeAllListeners<E extends keyof Events> (event: E): this
removeListener<E extends keyof Events> (event: E, listener: Events[E]): this
emit<E extends keyof Events> (event: E, ...args: Arguments<Events[E]>): boolean
eventNames (): (keyof Events)[]
listeners<E extends keyof Events> (event: E): Function[]
listenerCount<E extends keyof Events> (event: E): number
getMaxListeners (): number
setMaxListeners (maxListeners: number): this
}
// MY CODE
export const awaitNextEvent = <
Events extends { [k: string]: Function },
Event extends keyof Events
>(
eventEmitter: TypedEventEmitter<Events>,
event: Event,
) =>
// Type 'Events[Event]' does not satisfy the constraint '(...args: any) => any'.
new Promise<Parameters<Events[Event]>>(resolve => {
// Type '(...args: any[]) => void' is not assignable to type 'Events[Event]'
const cb: Events[Event] = ((...args) => {
resolve(args);
});
eventEmitter.once(event, cb);
});
const t:TypedEventEmitter<{'ev':(arg:string)=>void,'ev2':()=>void}> = 1 as any
// ret is a Promise<[string]> as expected
const ret = awaitNextEvent(t,'ev')
后端(views.py):-
var data_to_send = {}; //push 'values to send' into this. $.ajax({
..... //other ajax attributes
data: {'val': JSON.stringify(data_to_send)},
..... //other ajax attributes });
答案 3 :(得分:-4)
创建一个表单,其数据为CharField
或TextField
类型的字段,并验证传递的数据。 Similar SO Question