有没有办法打印参数以便在Django中调试视图? 我尝试过以下方法:
def print_request(request):
f = open('some_file.txt', 'w+')
f.write("testing")
for key in request.POST:
value = request.POST[key]
f.write(value)
f.close()
return HttpResponse("Done")
该函数返回“Done”值,该文件是在里面进行测试创建的,但是请求中没有任何内容,这就是我在Objective-c中传递的内容
-(void)callServer
{
NSArray *objects = [NSArray arrayWithObjects:@"string", @"string", @"string", nil];
NSArray *keys = [NSArray arrayWithObjects:@"description", @"icon", @"showOnMap", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];
id json = [NSJSONSerialization
dataWithJSONObject:questionDict
options:0
error:nil];
NSLog(@"jsonRequest is %@", json);
NSURL *url = [NSURL URLWithString:test_POST_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *requestData = [NSData dataWithBytes:[json UTF8String] length:[json length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSError* error;
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
NSDictionary* jsonAddressResult = [NSJSONSerialization JSONObjectWithData:requestData options:kNilOptions error:&error];
}
}
无法弄清楚为什么这不起作用
答案 0 :(得分:0)
Python有一个完整的日志记录模块作为stdlib的一部分,对于最新版本的Django,它已经部分配置为向stderr发送日志消息。这是标准的官方和RightWay来记录任何python代码。现在您也可以直接写入stderr,但这对于生产代码来说并不是一个好主意。
另请注意,如果运行内置的deb服务器,您还可以在代码中添加断点,以直接检查请求(以及整个调用堆栈中的任何内容)。