form.as_table in django - django

form = EmailForm()
return render_to_response('books/send_mail.html', {'email_form': form})
When i am using form.as_table in my template, all the fields are getting rendered in the same line : (Email: Subject: Message: ).
How can i render these form fields in seperate lines using as_table. I dont want to use as_p or as_ul, because they do not have proper alignment.

Did you wrap your form in table tags?
You can also use form.as_table to output table rows (you'll need to provide your own tags)

Related

How can I see all possible attributes of an context variables in django templates

I am trying to use double model form at once in one single view, I am using django-betterforms to merge those, It merged all fields from two model in one single form. I know I can use different class and id to separate them, But I can't extract them in template form, like
{{ form }}
it will place full form in template, I can render all field like this
{% for field in form %}
{{ field }} or anything
{% endfor %}
My question is how can I know all the possible attributes of this field like
{{ field.* }} *=anything
kind of dir(field).
This is a problem I have facing but what will be solution to find all attributes or separate two forms in two side. Basically I need to separate two model, those will save in same time with same view but in front-end those will be different.
Thanks in advance!!!
You create a custom filter:
in templatetags/my_filters.py:
from django import template
register = template.Library()
#register.filter
def getallattrs(value):
return dir(value)
in your template:
{% load my_filters %}
...
{{ field|getallattrs }}

How can I render new HTML content in Django template?

I'm a beginner in Django.
In my template file, I have the following:
<select id="selectbox"></select>
<div id="containerdiv"></div>
Within the containerdiv, I can return a HttpResponse such as:
<form> <!-- some more input elements here --> </form>
so now I will have a new form within the div. The content of the form will change depending on the value of selectbox. However, I also have {% csrf_token %} tag within the rendered HttpResponse which is not being rendered properly. Is this even the correct way to work with Django?
You could load as many forms in the page by the following method
def formPage(request):
form1=FormA()
form2=FormB()
#as many as you want
return render(request,"example.html",{"form1"=form1,"form2":form2})
Then call the forms in the HTML pages as per the name specified in the view, and use vanilla JS to render form as per choice selected in the choice box
You can refer the following link: JS content manipulation

Django: The page shows "(<django.forms.fields.BooleanField object at ...>)" at variable location in a template

I was trying include a checkbox on my template, but it showed me some text on the browser instead.
I have a form (forms.py) that contains boolean field, e.g:
Class MyForm(forms.Form):
my_checkbox = forms.BooleanField(required=False, label='my_checkbox'),
and I have the following in my templates/my_app/my_form_template.py:
<form id="form" method="post">
{{ form.my_checkbox }}
<form>
In the browser after running the server, I've got:
(<django.forms.fields.BooleanField object at 0x0000014BE3CF9208>,)
The CharField and ChoiceField work perfectly except BooleanField. What is a way to represent a checkbox on the template?
I've tried {{ form }} in the template and it shows the checkbox, but I wish to custom the attributes of the checkbox.
There is a comma at the end of the field that causes an error. Removing it would fix the problem.

Show error in templates django

im using the form.add_error option of django to add customized errors to
form checks.
when i use the add_error in the ModelForm the errors are shown in the
template, but when i use it in the view it doesn't..
what might be the problem?
the modelform add_error (in the clean method):
self.add_error('field', "fdgdsfgfds.")
the view add_error:
def form_valid(self, form):
form.add_error('field', "sfsfdsfsd.")
return self.form_invalid(form)
in the template:
{{ form.errors }}
{{ field.errors }}
{{ form.non_field_errors }}
{{ form.as_table }}
Django model forms have a certain way of working and this in essence is a good thing because it makes sure your code stays clean and that each one of your methods have a standardized flow control.
The forms clean and clean_fieldname methods should be where all of your validation is done, these methods get called from the forms is_valid method and all contribute with their errors towards statings whether the form is valid or not. See my other answer for a more detailed explanation as to how is_valid actually calls the clean methods
The form_valid and form_invalid are for you to handle in your view to handle what response should be shown. The django generic views by default will render success_url if the form is valid although you may find you want to override these methods to return a JsonResponse or similar to make your form more ajaxxy.
So any time you want to report errors, you need to use your clean methods. You may find in the future that you want to reuse a form and you don't want to repeat yourself in views by having to do validation again, so keeping all the validation contained in the form makes this possible. The views job is to handle where you want to display your form and what other context it would need.

Update non-request user information in a Django template and view

So I have a ManageUserForm in forms.py-- it renders correctly but it doesn't pull the right data from the user i'm trying to edit.
In the template, I have a for loop that works correctly
{% for tenants in tenants %}
{{ tenants.user }} {{ tenants.type }}
{% endfor %}
This template renders the list of objects in the UserProfile. And it does it correctly. The challenge I face is updating the "tenants.type" attribute. Again, the type shows up correctly but I don't know how to update it from this template page.
#views.py
def manage_users(request):
tenants = UserProfile.objects.all()
form = ManageUserForm(request.POST or None)
if form.is_valid():
update = form.save(commit=False)
update.save()
return render_to_response('manage_users.html', locals(), context_instance=RequestContext(request))
#forms.py
class ManageUserForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('full_name', 'user',)
`I think I need to call an instance but I have no idea how to do so for the non-request users AND still follow the pattern for the template. The template basically is a list of users where the request user (staff user) will be able to change the data in the list.
Thank you for your help!
You have one form for one user. You need a FormSet if you want to use that form to edit multiple tenants. Editing objects and displaying them are entirely different beasts; dont' confuse them.
formset = modelformset_factory(form=ManageUserForm, queryset=tenants)
Update:
You should have one {{ form.management_form }} and the rest of the {% for form in formset %}{{ form }}{% endfor %} in one <form> tag. All of your forms are the first form in the formset.
You should rewrite your template loop to iterate through formset forms instead of tenant objects. The tenant object can be accessed through {{ form.instance }}
Update 2:
You have an extra form because you probably haven't passed in the extra=0 parameter to the modelformset_factory function. These forms are typically used to add/edit data; thus it has support for adding N blank forms for creating.