Python / Django - 异常值:'WSGIRequest' 对象没有属性 'Meta'

Python / Django - Exception Value: 'WSGIRequest' object has no attribute 'Meta'

我愿意将所有 http 请求保存到数据库(request_method 代表数据库字段)并将它们打印到页面(例如,最后 10 个请求)但是我遇到了以下问题:异常值:'WSGIRequest' 对象没有属性 'Meta'。

models.py

from django.db import models

    class HttpRequest(models.Model):

        time = models.DateTimeField(auto_now=True, auto_now_add=False)
        request_method = models.CharField(max_length=20)

middleware.py

from .models import HttpRequest

class FirstMiddleware(object):

    def process_request(self, request):
        data = HttpRequest(request_method=request.Meta['REQUEST_METHOD'])
        data.save()

views.py

from django.shortcuts import render

def view_requests(request):
    request_list = HttpRequest.objects.all()[:10]
    return render(request, 'apps/hello/request_list', {'list': request_list})

这个问题发生在 middleware.py 文件的处理过程中(这就是为什么我不确定这里是否需要 view.py 但为什么不:))并且由于我是 django 的初学者,尽管任务似乎很容易,但我自己修复它是一个很大的挑战。真的很高兴你的见解。

不是Meta。是 <a href="https://docs.djangoproject.com/en/1.8/ref/request-response/#django.http.HttpRequest.META" rel="noreferrer">META</a>。希望对您有所帮助。