Re: Binding data to one o many forms from one view - BadHeaderError at - Header values can't contain newlines ()

2017-04-08 Thread Camilo Torres
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[\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)
>

Binding data to one o many forms from one view - BadHeaderError at - Header values can't contain newlines ()

2017-04-08 Thread Bernardo Garcia

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[\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:


{% csrf_token %}
{% if userprofile.user.is_student %}

My Student Profile data
{% bootstrap_form form_student %}
{% endif %}
 

{% if userprofile.user.is_professor %}
My Professor Profile data
{% bootstrap_form form_professor %}
{% endif %}