MyModelForm'对象没有属性'用户使用django [英] MyModelForm' object has no attribute 'user using django
问题描述
我要创建简单的django图像处理。首先,我已经在我的应用中创建了正确的django身份验证和多个上传图像。
现在,我希望用户可以从我创建的列表django表单中选择一个自己的图像,然后获取该图像以进行处理。我创建了一些东西,但没有用。
i want to create i simple django image processing .first i have create correct a django auth and multi upload images in my app. now i want the user can select one self image from a list django form where i create and that image i get for my processing.i create something but not work.
我遇到了这个错误:
'MyModelForm' object has no attribute 'user'
此处的代码:
views.py
@login_required(login_url="login/")
def myview(request):
Myf = MyModelForm(request.user,request.POST)
return render(request,'home.html',{'Myf':Myf})
forms.py
class MyModelForm(ModelForm):
def __init__(self, *args, **kwargs):
if 'user' in kwargs:
self.user = kwargs.pop('user')
super(MyModelForm, self).__init__(*args, **kwargs)
choices = [(obj.id, obj.upload.url) for obj in MyModel.objects.filter(user=self.user)]
self.fields['upload'].widget = Select(choices=choices)
class Meta:
model = MyModel
fields = ('upload',)
如果我将 Myf = MyForm(request.user,request.POST)
替换为 Myform = MyModelForm( user = request.user)
,然后我认为列表的工作原理如下:
if i replace Myf = MyForm(request.user,request.POST)
with Myform = MyModelForm(user=request.user)
then i think list worked like this
但是我不能选择图像进行图像处理。
but i cant take select image for image processing.
html:
<form class="" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ Myform}}
<input type="submit" name="" value="Submit">
</form>
任何想法吗?
推荐答案
您的 MyForm
类在 __ init __ <中没有
user
/ code>方法的签名,因此在实例化表单时需要将其作为关键字参数提供:
Your MyForm
class does not have user
in the __init__
method's signature, so you need to provide it as a keyword argument when instantiating the form:
MyForm(user=request.user,data=request.POST)
这篇关于MyModelForm'对象没有属性'用户使用django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!