我有一个应用程序,可以让当前登录的用户创建一个Person对象。该人员对象拥有当前登录用户作为其所有者。 但是当我保存表格时,它给了我以下错误
plus
我正在尝试使用 Django CreateView
错误引用
implicit class QuaternionSupport(quaternions: SU_n[2]) {
def plus(quaternion1: Quaternion, quaternion2: Quaternion) : Quaternion = ???
}
Views.py
global name 'request' is not defined
models.py
Traceback:
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\edit.py" in post
217. return super(BaseCreateView, self).post(request, *args, **kwargs)
File "C:\Users\BITSWI~1\Desktop\Maala\Maala\lib\site-packages\django\views\generic\edit.py" in post
183. return self.form_valid(form)
File "C:\Users\Bitswits 3\Desktop\Maala\MaalaWeddings\userfiles\views.py" in form_valid
120. obj.user_relation = request.user
Exception Type: NameError at /Personadd/
Exception Value: global name 'request' is not defined
我什至无法理解为什么这个错误首先出现,因为我认为 request 在Django中随处可见
答案 0 :(得分:1)
使用Class Based View
您要使用的所有参数或变量都需要自身附加
所以,
obj.user_relation = self.request.user
self
代表instance of the class
。通过使用“自我” 关键字,我们可以访问类的属性和方法。它将属性与给定参数绑定。