Django WSGIRequest args错误

时间:2014-01-16 21:30:25

标签: python django django-wsgi

我明白了 AttributeError("'WSGIRequest' object has no attribute 'args'",) 当我尝试拨打电话时

GET /sign_s3/?s3_object_type=image/jpeg&s3_object_name=default_name

我错过了什么吗?

除了请求之外,我还需要包含*args, **kwargs吗?

这是追溯:

            response = middleware_method(request, callback, callback_args, callback_kwargs)
            if response:
                break
    if response is None:
        wrapped_callback = self.make_view_atomic(callback)
        try:
            response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
        except Exception as e:
            # If the view raised an exception, run it through exception
            # middleware, and if the exception middleware returns a
            # response, use that. Otherwise, reraise the exception.
            for middleware_method in self._exception_middleware:
                response = middleware_method(request, e)

观点:

@login_required()
def sign_s3(request):
    object_name = request.args.get('s3_object_name') 

3 个答案:

答案 0 :(得分:4)

HttpRequest对象没有args属性。

你应该使用

request.GET.get('s3_object_name')

获取s3_object_name value

Django文档在HttpRequest上有一个很好的section

答案 1 :(得分:0)

HttpRequest对象具有类似字典的对象id没有args属性

如果密钥是必需的并且存在于请求对象中,则可以使用:

request.GET中[ 's3_object_name']

  

这将返回if键存在的值。如果不是那么一个例外

     

如果您的钥匙是可选的:

request.GET.get( 's3_object_name')

答案 2 :(得分:0)

与此erreur相同: 除了User.DoesNotExist:属性错误:'function'对象没有属性'DoesNotExist'

class UserPosts(generic.ListView):
   model = models.Post
   template_name = 'posts/user_post_list.html'

def get_queryset(self):
    try:
        self.post_user = User.objects.prefetch_related("posts").get(
            username__iexact=self.kwargs.get("username")
        )
    except User.DoesNotExist:
        raise Http404
    else:
        return self.post_user.posts.all()

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["post_user"] = self.post_user
    return context

am通过以下方式解决它:

从django.contrib.auth导入get_user_model

User = get_user_model()