django - Custom UpdateView in Python -
i trying custom updateview work in python/django. believe code i've writtten correct, seems returning proper primary key id in url when click on associated dropdown. problem not seeing of data associated record on screen in update mode. screen appears in edit mode, there no data. suspect problem perhaps django template in html form? however, have played form , used {{ form }} , returns blank form. i've played afternoon , i'm out of guesses. here view:
def updating_document(request, pk): doc = get_object_or_404(doc, pk=pk) form = update_doc_form(request.user, request.post) if request.method == 'get': if form.is_valid(): form.save() return httpresponseredirect(reverse('app:main_procedure_menu')) else: print("form invalid!") return render(request,'doc/update_doc.html',{'form':form })
i have associated form...
form.py
class update_doc_form(forms.modelform): class meta: model = doc exclude = ['user'] doc_name = forms.charfield(widget=forms.textinput) description = forms.charfield(required=true,widget=forms.textarea) team = forms.charfield(widget=forms.select) document = forms.charfield(required=true,widget=forms.textarea) def __init__(self, *args, **kwargs): super(update_doc_form, self).__init__(*args, **kwargs) self.fields['doc_name'].widget.attrs['class'] = 'name' self.fields['description'].widget.attrs['class'] = 'description' self.fields['team'].widget.attrs['class'] = 'choices' self.fields['team'].empty_label = ''
i'm newbie, want use custom updateview can alter of fields , pass user information. feel code close, need figure out why it's not populating form data. in advance help!
what difference day makes. found answer on morning. not sure how credit person or issue number....
the answer add following line of code form:
user = kwargs.pop('object_user')
i needed add following function view:
def get_form_kwargs(self): kwargs = super(viewname,self).get_form_kwargs() kwargs.update({'object_user':self.request.user}) return kwargs
this question answered in 2013 ivan virabyan. ivan!
i went standard class based updateview , scrapped plans custom updateview once able figure out how use class based view(updateview) , "pop" off user information when passing form based on ivan virabyan's answer in 2013.
the code above allows user not pass modelform don't unexpected user error.
Comments
Post a Comment