Django:参数输出

2024-03-29 01:07:05 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有打印参数的方法来调试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];

    }
}

我不明白为什么这样不行


Tags: keyjsonstringrequesterrorpostlengthwrite
1条回答
网友
1楼 · 发布于 2024-03-29 01:07:05

Python有一个比较完整的日志模块作为stdlib的一部分,对于Django的最新版本,它已经部分配置为向stderr发送日志消息。这是在任何python代码中进行日志记录的标准和正确的方法。现在您也可以直接简单地编写stderr,但这对于生产代码来说并不是一个好主意。你知道吗

还要注意,如果运行内置的deb服务器,还可以在代码中添加断点,直接检查请求(以及整个调用堆栈中的任何内容)。你知道吗

相关问题 更多 >