Hi,

Inside your "post" method, you have this line:
context = super(AccountProfilesView, self).post(request, *args, **kwargs)
in that case "context" is a HttpResponse object[0]. May be, from the name 
you use for the variable, you think this is a simple dict as those used to 
pass context to render functions; it is not.
You can use HttpResponse as a dictionary to put HTTP headers in the 
response [1], so line 184 is setting an HTTP header with a value of the 
HTML to render the form; that is not what you want.
You have to remove that line and create your context with something like:
context = {}
And you have to return an HttpResponse object, probably by calling one of 
the render functions [2].


[0] 
https://docs.djangoproject.com/en/1.11/ref/request-response/#httpresponse-objects
[1] 
https://docs.djangoproject.com/en/1.11/ref/request-response/#setting-header-fields
[2] https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render

On Saturday, April 8, 2017 at 5:06:34 PM UTC-4, Bernardo Garcia wrote:
>
>
> I have the following forms to each user profile
>
>     class UserUpdateForm(forms.ModelForm):
>         class Meta:
>             widgets = {'gender':forms.RadioSelect,}
>             fields = ("username", "email", "is_student",           
> "is_professor", "is_executive",)
>             model = get_user_model() #My model User
>     
>     class StudentProfileForm(forms.ModelForm):
>         class Meta:
>             model = StudentProfile
>             fields = ('origin_education_school',current_education_school',
>                 'extra_occupation')
>     
>     class ProfessorProfileForm(forms.ModelForm):
>         class Meta:
>             model = ProfessorProfile
>             fields = ('occupation',)
>     
>     class ExecutiveProfileForm(forms.ModelForm):
>         class Meta:
>             model = ExecutiveProfile
>             fields = ('occupation', 'enterprise_name', 
> 'culturals_arthistic','ecological')
>
> I have an URL which call to my AccountProfilesView class based view which 
> create an instance of the previous forms according to the user profile:
>
>         url(r"^profile/(?P<slug>[\w\-]+)/$",
>             views.AccountProfilesView.as_view(),
>                 name='profile'
>         ),
>
>
> My AccountProfilesView  is this:
>
> I this moment, from the AccountProfilesView class based view I am create 
> the different instances of each one of these forms, according to the 
> user profile, then, if an user have the is_student profile their related 
> form will be generated, and so, of this way to is_professor  and 
> is_executive profiles
>
> If an user have the three profiles (is_student, is_professor,is_executive ) 
> in one single form will be created or rendered the fields of the three 
> forms associated to each user profile related.
>
> class AccountProfilesView(LoginRequiredMixin, UpdateView):
>         # All users can access this view
>         model = get_user_model()
>         #success_url = reverse_lazy('dashboard')
>         template_name = 'accounts/profile_form.html'
>         fields = '__all__'
>     
>         def get_context_data(self, **kwargs):
>             context = super(AccountProfilesView, 
> self).get_context_data(**kwargs)
>             user = self.request.user
>     
>             if not self.request.POST:
>                 if user.is_student:
>                     profile = user.get_student_profile()
>                     context['userprofile'] = profile
>                     context['form_student'] = forms.StudentProfileForm()
>                 if user.is_professor:
>                     profile = user.get_professor_profile()
>                     context['userprofile'] = profile
>                     context['form_professor'] = 
> forms.ProfessorProfileForm()
>                     print ("profesor form is", context['form_professor'])
>                 if user.is_executive:
>                     profile = user.get_executive_profile()
>                     context['userprofile'] = profile
>                     context['form_executive'] = 
> forms.ExecutiveProfileForm()
>             return context
>     
>         def post(self, request, *args, **kwargs):
>             self.object = self.get_object()
>             context = super(AccountProfilesView, self).post(request, 
> *args, **kwargs)
>             user = self.request.user
>             # if self.request.method == 'POST':
>             if user.is_student:
>                 context['form_student'] = forms.StudentProfileForm(
>                     self.request.POST)
>             elif user.is_professor:
>                 context['form_professor'] = forms.ProfessorProfileForm(
>                     self.request.POST)
>             elif user.is_executive:
>                 context['form_executive'] = forms.ExecutiveProfileForm(
>                     self.request.POST)
>             return context
>     
>         def form_valid(self, form):
>             context = self.get_context_data(form=form)
>             user = self.request.user
>             user = form.save()
>             if user.is_student:
>                 student = context['form_student'].save(commit=False)
>                 student.user = user
>                 student.save()
>             if user.is_professor:
>                 professor = context['form_professor'].save(commit=False)
>                 professor.user = user
>                 professor.save()
>             if user.is_executive:
>                 executive = context['form_executive'].save(commit=False)
>                 executive.user = user
>                 executive.save()
>             return super(AccountProfilesView, self).form_valid(form)
>     
>         def get_success_url(self):
>             return reverse('dashboard')
>
>  And in my template, I have the following small logic:
>
>         <form method="POST">
>             {% csrf_token %}
>             {% if userprofile.user.is_student %}
>         
>             <div align="center"><i>My Student Profile data</i></div>
>                 {% bootstrap_form form_student %}
>             {% endif %}
>              
>             
>             {% if userprofile.user.is_professor %}
>                 <div align="center"><i>My Professor Profile data</i></div>
>                 {% bootstrap_form form_professor %}
>             {% endif %}
>             
>             
>             {% if userprofile.user.is_executive %} 
>                 <div align="center"><i>My Executive Profile data</i></div> 
>  
>                 {% bootstrap_form form_executive %}
>             {% endif %}
>     
>             <input type="submit" value="Save Changes" class="btn 
> btn-default">
>         </form>
>
>
> From the perspective of show the forms with the fields according to the 
> userprofile, this approach works, the data or fields related to the user 
> profile are denoted or renderised
>
> For example this user have the three profiles  and in the screen of 
> profile in the application, show the three forms:
>
> <https://i.stack.imgur.com/lUfRL.jpg>
>
>
> But at moment of perform update of such view screen form, (in which each 
> profile have their respective model/table in where their own data are 
> managed)
> happened the following:
>
> When I click en *Save Changes *there is an error:
>
> File "/home/bgarcial/workspace/ihost_project/accounts/views.py", line 185, 
> in post
>         self.request.POST)
>       File 
> "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/http/response.py",
>  
> line 142, in __setitem__
>         value = self._convert_to_charset(value, 'latin-1', 
> mime_encode=True)
>       File 
> "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/http/response.py",
>  
> line 115, in _convert_to_charset
>         raise BadHeaderError("Header values can't contain newlines (got 
> %r)" % value)
>     django.http.response.BadHeaderError: Header values can't contain 
> newlines (got '<tr><th><label for="id_origin_education_school">Origin 
> education institute:</label></th><td><input id="id_origin_education_school" 
> maxlength="128" name="origin_education_school" type="text" 
> value="Universidad de la Amazonía" required /></td></tr>\n<tr><th><label 
> for="id_current_education_school">Current education 
> institute:</label></th><td><input id="id_current_education_school" 
> maxlength="128" name="current_education_school" type="text" 
> value="Universida EAFIT" required /></td></tr>\n<tr><th><label 
> for="id_extra_occupation">Extra occupation:</label></th><td><input 
> id="id_extra_occupation" maxlength="128" name="extra_occupation" 
> type="text" value="Mother" required /></td></tr>')
>     [08/Apr/2017 20:21:13] "POST /accounts/profile/luisa/ HTTP/1.1" 500 
> 108206
>
> More precisely:
>
> <https://i.stack.imgur.com/g7dhD.png>
>
>
> I think so, is that at the moment of create the form instances of the 
> Django forms, and I denote that the data to be included in the request  and 
> the POST operation, this is not validated of some way
>  
> I unknown the traceback error:
>
> BadHeaderError
> Header values can't contain newlines (got '<tr><th>) ..... 
>
> I've searched that error, but I don't have related information about it.
>
> Best Regards
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5f19a008-2c3a-4647-b4e2-d2bbce16d4d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to