python form-get-current-user.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python form-get-current-user.py相关的知识,希望对你有一定的参考价值。

# views.py
class UserProfileUpdate(LoginRequiredMixin, UpdateView):
    
    # ...

    # Pass current user to form through `initial`
    def get_form_kwargs(self, **kwargs):
        kwargs = super(UserProfileUpdate, self).get_form_kwargs(**kwargs)
        kwargs['initial']['user']=self.request.user
        return kwargs

    # ...


# forms.py
class UserUpdateForm(forms.ModelForm):
	
	# ...

	def __init__(self, *args, **kwargs):
		
		# Getting the user calling the parent save method, 
		# might trigger an unhandled error. 
		# 	
		# 	user = super(UserUpdateForm, self).save(commit=False)
		# 	
		# So, we could get the current user from the `initial` attrbute
		# set in the `views.py`. 
		self.user = self.initial['user']

以上是关于python form-get-current-user.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识