由于Django 1.5原始帖子数据可以通过request.body访问.
在我的应用程序中,我有时会通过表单和有时原始数据(例如json)发送数据.有没有办法写这样一个不会失败的函数?
def get_post_var(request, name):
result = request.POST.get(name)
if result:
return result
post_body = dict(urlparse.parse_qsl(request.body))
result = post_body.get(name)
if result:
return result
return None
Run Code Online (Sandbox Code Playgroud) 寻找一种在我的错误日志中添加标题、正文和用户电子邮件地址的方法,以及我的views.py 中异常的堆栈跟踪
在网上搜索了几个小时后,许多人建议编写自己的中间件,有些人建议将此类信息记录到单独的日志中。然而,知道你的代码出错的地方解决了问题的一部分,确定它影响了哪个可怜的灵魂以及在该异常期间发送了哪些请求数据对于纠正问题有很长的路要走。将这些信息放在同一个日志文件中对我来说很有意义。
目前在我的 views.py 中,我有这个简单的设置:
from django.db.models import Min, Max, Q, F, Count, Sum
from django.db import connection
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from myapp.models import *
import logging
logging.basicConfig(filename="errors.log",
level=logging.ERROR,
format='%(asctime)s: %(message)s')
def random_view(request):
if request.user.is_authenticated() and request.user.is_active:
# generic view code goes here.
else:
return HttpResponse(status=401)
Run Code Online (Sandbox Code Playgroud)
这种设置在一段时间内运行良好。每次出现异常时,它都会注销时间、异常错误消息和堆栈跟踪。
如何添加 request.META、request.user.id 和 request.body 以及堆栈跟踪?
任何建议都会有所帮助。一个有效的答案,甚至更好!
谢谢