Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:模块';urllib.request';没有属性';用户';_Python_Django - Fatal编程技术网

Python 属性错误:模块';urllib.request';没有属性';用户';

Python 属性错误:模块';urllib.request';没有属性';用户';,python,django,Python,Django,我正在尝试创建文章,所以我需要请求当前用户的信息 user = User.objects.get(id=request.user.id) profile = Profile.objects.filter(user=user).get() instance = profile 在尝试执行此操作时,会出现以下错误: File "C:\...app\views.py", line 41, in aRTİCLECreateView user = User.objec

我正在尝试创建文章,所以我需要请求当前用户的信息

    user = User.objects.get(id=request.user.id)
    profile = Profile.objects.filter(user=user).get()  
    instance = profile
在尝试执行此操作时,会出现以下错误:

File "C:\...app\views.py", line 41, in aRTİCLECreateView
    user = User.objects.get(id=request.user.id)
AttributeError: module 'urllib.request' has no attribute 'user'


在基于类的视图中,可以使用
self.request
访问请求对象(因为它是视图实例的成员)

通过编写
request.user
,您的IDE可能从
urllib
进行了导入:

    user = User.objects.get(id=self.request.user.id)
    profile = Profile.objects.filter(user=user).get()  
    instance = profile

请注意,您只能在方法中编写这些语句,而不能在类级别编写,因为在该级别,在基于类的视图中没有
self
,您可以使用
self.request
访问请求对象(因为它是视图实例的成员)

通过编写
request.user
,您的IDE可能从
urllib
进行了导入:

    user = User.objects.get(id=self.request.user.id)
    profile = Profile.objects.filter(user=user).get()  
    instance = profile

请注意,您只能在方法中编写这些语句,而不能在类级别编写,因为在该级别,没有
self

这是一个基于类的视图?这是CreateApiView这是一个基于类的视图?这是CreateApiView现在它给我一个错误t说self没有定义?@UmarBeyoğlu:因为你可能在类级别定义它。你需要用一种方法来定义它,就像在答案底部指定的那样。我很困惑你在说什么,但现在问题解决了。首先,我认为我必须在模型中创建self函数,但我必须使用一个定义self的函数,比如create(self,等等)。谢谢!现在它给了我一个错误,说自我没有定义?@UmarBeyoğlu:因为你很可能在班级层面上定义自我。你需要用一种方法来定义它,就像在答案底部指定的那样。我很困惑你在说什么,但现在问题解决了。首先,我认为我必须在模型中创建self函数,但我必须使用一个定义self的函数,比如create(self,等等)。谢谢!