How can a django forms.CharField generate a required input? - python

I'm a new pie to django and trying to use forms.CharField to generate a required input.
By default, a forms.CharField is translated as:
<input id="id_mail_to" name="mail_to" type="text" />
But I want a:
<input id="id_mail_to" name="mail_to" type="text" required/>
How can I get that?

Use attrs
Try this,
mail_to=forms.CharField(required=True, widget=forms.TextInput(attrs={'required': "required"}))
You can see this,
<input id="id_mail_to" name="mail_to" required="required" type="text">
JsFiddle

You can try like this in forms.py
mail_to= forms.CharField(required=True, label="mail to")

Related

BUMP - django form request.POST.get('field-name', '') always empty

Trying to get some values from a form but the parameters are always empty. here is the path from my urls.py:
url((r'^partners-email$'), views.partners_email, name="Partners Email"),
This is a simple form I have in the template:
<form method="POST" action="/partners-email">
<input name="email" class="form-control" id="client-email">
<input type="submit" value="Submit" />
</form>
and here is my function in views.py:
def partners_email(request):
from_email = request.POST.get('email', '')
print('MY_TAG: ' + from_email)
output is always:
"MYTAG: "
any ideas?
Thank you very much in advance
Your input elements don't have a name attribute so the browser will never send data for them.
Try it.
<input type="text" name="email" class="form-control" id="client-email">

WTForms - POSTing text fields to an array

In regular HTML, you can have multiple fields POST to an array:
<input type="text" name="arr[]">
<input type="text" name="arr[]">
<input type="text" name="arr[]">
<input type="text" name="arr[]">
How can I get this functionality from WTForms? Basically, I have a form where users and click on little plus and minus buttons to add or remove fields from the form.
You're looking for WTForm FieldList. It allows you to create an arbitrary list of the same field.
Ex.
emails = FieldList(StringField('email'), min_entries=1, max_entries=5)

How to add help_text to display as input value?

I would like to get the help_text declared in my form class to render inside the HTML form element rather than Django's default, which displays it as a separate element. Specifically, for textarea fields the help_text would need to go between the opening and closing HTML tags and for input fields the help_text would need to be set as the value= attribute -- basically, turning:
text = forms.CharField(widget=forms.Textarea(attrs={'class':'form-control', 'rows':2}), help_text="Some help text")
image = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}), help_text="More help text")
into:
<textarea class="form-control" id="id_text" name="text" rows="2">Some help text</textarea>
<input class="form-control" id="id_image" name="image" value="More help text">
As is, the first block of code does not insert the help_text anywhere.
One way to do it would be to just use template tags to insert everything inline, but this feels like a hack.
<textarea class="form-control" id="{{ form.text.auto_id }}" name="{{ form.text.html_name }}" rows="2">{{ form.text.help_text }}</textarea>
<input class="form-control" id="{{ form.image.auto_id }}" name="{{ form.image.html_name }}" value="{{ form.image.help_text }}">
I figure there's gotta be a better way?
Instead of using the help_text, use placeholder attribute.
text = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Some help text'}))
This answer might be too late...but for others who may need.
If for instance, you got a model field name job_summary, then you may do this in your forms.py:
class JobForm(forms.Form):
model = Job
...
class Meta:
widgets = {'job_summary', forms.Textarea(attrs={'placeholder': Job._meta.get_field('job_summary').help_text}), }

HTML input array parsing in Python (GAE)

I'm two days in to Python and GAE, thanks in advance for the help.
I have an input array in HTML like this:
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
I want to parse the input in Python, and I'm trying this, which isn't working:
items = self.request.get('p_item')
for n in range(1,len(items)):
self.response.out.write('Item '+n+': '+items[n])
What is the correct way to do this?
Change your html to this
<input type="text" name="p_item">
<input type="text" name="p_item">
<input type="text" name="p_item">
and use the self.request.get_all() method http://code.google.com/appengine/docs/python/tools/webapp/requestclass.html#Request_get_all
p.s. For reference, there is no concept of arrays for GET/POST data, your form gets transformed a key=value string separated by '&' e.g.
p_item=1&p_item=3&p_item=15
etc, it's up to the web framework to interpret whether a parameter is an array.
Edit: oops, just read the comments that you figured this out already, oh well :P
I would recommend doing some debugging if this sort of issue comes up. Make things simple and write out your variable values and ensure you get what you expect at each step. Do something like the following:
<form method="get">
<input type="text" name="single_key" />
<input type="text" name="array_key[some_key]" />
<input type="submit" />
</form>
And see what happens when running the following Python on the backend:
single_value = self.request.get('single_key')
self.response.out.write(str(single_value))
array_value = self.request.get('array_key')
self.response.out.write(str(array_value))
Based on the output you should have a better idea of what to get the desired results or how to add more detail to your question if you still don't understand a certain behavior.

HTML form name array parsing in Pyramid (Python)

Is there any way for Pyramid to process HTML form input which looks like this:
<input type="text" name="someinput[]" value="" />
or even more usefully:
<input type="text" name="someinput[0][subelement1]" value="" />
<input type="text" name="someinput[0][subelement2]" value="" />
<input type="text" name="someinput[1][subelement1]" value="" />
<input type="text" name="someinput[1][subelement2]" value="" />
...and access that data easily (e.g. via a dict)?
Any help would be much appreciated!
EDIT: to make it clearer, what I need is the ability to have a form where a user can add as many 'instances' of a group of input elements, e.g. adding between 1 and n users, each containing a firstname, lastname, username (or something like that).
One solution would be to use peppercorn. Although it does not support the syntax you're looking for, it will let you send structured data to your Pyramid application through the use of forms. A more casual description exists too.

Categories

Resources