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}), }
Related
So, I've hacked this together from a few sources, so if I'm totally going about it the wrong way I welcome feed back. It also occurs to me that this is not possible, as it's probably a security check designed to prevent this behavior being used maliciously.
But anyway:
I have a form on our Django site where people can request to change the name of one of our items, which should automatically create a jira ticket. Here's the form:
<form target="_blank" action='http://issues.dowjones.net/secure/CreateIssueDetails!init.jspa' method='get' id='create_jira_ticket_form'>
<a id='close_name_change_form' class="close">×</a>
<label for="new_name">New name: </label>
<input id="new_name" type="text" name="new_name" value="{{item.name}}">
<input type="hidden" value="10517" name="pid">
<input type="hidden" value="3" name="issuetype">
<input type="hidden" value="5" name="priority">
<input type="hidden" value="Change name of {{item.name}} to " name="summary" id='summary'>
<input type="hidden" value="{{request.user}}" name="reporter">
<input type="hidden" value="user123" name="assignee">
<input type="hidden" value="" name="description" id="description">
<input id='name_change_submit' class="btn btn-primary btn-sm" type="submit" value="Create JIRA ticket">
</form>
Then I have a little JS to amend the fields with the new values:
$(document).ready(function(){
$('#create_jira_ticket_form').submit(function(){
var watchers = ' \[\~watcher1\] \[\watcher2\]';
var new_name = $('#new_name').val();
var summary = $('#summary').val();
$('#summary').val(summary + new_name);
$('#description').val(summary + new_name + watchers);
})
})
It comes very close to working, but the description field is escaped, leaving it looking like:
Change name of OLDNAME to NEWNAME %5B%7Ewatcher1t%5D %5B%7Ewatcher2%5D
Which is less than helpful. How can I keep it as is so I can add watchers?
This happens when your form encodes the fields and values in your form.
You can try this out by this simple snippet:
console.log($('form').serialize());
you should see something like
description=ejdd+%5B~watcher1%5D+%5Bwatcher2%5D
in order to prevent this you should change your method='get' to method='post'.
The encoding happens because it's apart of HTTP, read here why
You can also read the spec paragraph
17.13.3 Processing form data
I would like to create a 'save as draft' feature for a long entity form.
Consider the data structure example below:
class BigEntity(ndb.Model):
title = ndb.StringProperty()
date = ndb.DateTimeProperty()
# Many more properties ...
The form.html:
<form method="post">
Title: <input type="text" name="title">
<!-- Very long form -->
Submit <input type="submit" value="Submit">
Save As Draft <input type="submit" value="Save As Draft">
</form>
What is a solid way to build a 'save as draft' feature for this?
You can simply add a property isDraft to your entity and set it to true or false depending on which button was pressed.
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")
Here is how my models are setup:
class ModelA(models.Model):
user = models.ForeignKey(User)
modelB = models.ForeignKey('ModelB', related_name='modelB')
# more fields here
class ModelB(models.Model):
user = models.ForeignKey(User)
# more fields here
The user field in both classes are just so that both of these can be queried by the same user later on.
I have a ModelForm of ModelB and I would like to have the users be able to fill out a ModelB ModelForm with a dynamic number of ModelA instances. I would like ModelB's form to start out with 1 instance of ModelA to fill out and then the user should be able to add more ModelA's to ModelB's form.
I'm stuck on this as a personal project and would really like to move forward. Thanks for any help!
Edit 1: I know that I will most likely need to do some work by overloading ModelForms def __init__, but that is about as far as I have gotten. Also I am using ClassBasedGeneric Views (CreateView to get the add working and an EditView later on), but I am willing to move away from that if needed.
You require django inline formset
In view:
f_set = inlineformset_factory(ModelB, ModelA, extra=1)
In template lets suppose you are rendering form_set in a div whose id is one_to_many:
<form>
<div id="one_to_many">
{{f_set.as_table}}
</div>
</form>
#This will render some thing like this
<form>
<div id="one_to_many">
<table>
<tr>
<th>
<label for="id_form-0-field_name">Field Name:</label>
</th>
<td>
<input id="id_form-0-field_name" type="text" name="form-0-field_name" value="" maxlength="100" />
<input type="hidden" name="form-0-id" value="1" id="id_form-0-id" />
</td>
</tr>
</table>
<input type="button" value="Add More Field" id="add_more">
</div>
</form>
Now the jquery part to add field dynamically:
$(document).ready(function(){
$('#add_more').click(function(){
var tbl = $('#one_to_many').children('table');
var last_id = parseInt(tbl.find('input[type=hidden]').last().val());
var next_id = last_id + 1;
htm = '<tr><th><label for="id_form-'+last_id+'-field_name">Field Name:</label></th><td><input id="id_form-'+last_id+'-field_name" type="text" name="form-'+last_id+'-field_name" value="" maxlength="100" /><input type="hidden" name="form-'+last_id+'-id" value="'+next_id+'" id="id_form-'+last_id+'-id" /></td></tr>'
tbl.find('tr').last().after(htm);
});
});
You can test this on jsfiddle. Note this will only work if you render your form as_table
I have a form with generic fields
forms.py
class OfferForm(forms.ModelForm):
class Meta:
model=Offer
some_views.py
def add_field(request):
form = OfferForm()
#some logic here
for x in list:
form.fields[x]=forms.ModelChoiceField(queryset=some_query)
return render_to_response(template,{'form':form,'list_of_add_field':list}
So , In my template i want to do something like this:
{%for x in list_of_add_field%}
Name add field is {{x}}
Choices:
{%for y in form.{{x}}.choices %}
<input type="checkbox" name="form.{{x}}.html_name">y </input>
{%endfor%}
{%endfor%}
How can do that ? Any idea ?
Thank you !
I see you are intended to do and here my reply. You should adapt your code to django and not django to your code. In this way, to solve your issue, the 'prefix' form resource is the right option:
You can put several Django forms inside one tag. To give each Form its own namespace, use the prefix keyword argument:
>>> mother = PersonForm(prefix="mother")
>>> father = PersonForm(prefix="father")
>>> print mother.as_ul()
<li><label for="id_mother-first_name">First name:</label> <input type="text" name="mother-first_name" id="id_mother-first_name" /></li>
<li><label for="id_mother-last_name">Last name:</label> <input type="text" name="mother-last_name" id="id_mother-last_name" /></li>
>>> print father.as_ul()
<li><label for="id_father-first_name">First name:</label> <input type="text" name="father-first_name" id="id_father-first_name" /></li>
<li><label for="id_father-last_name">Last name:</label> <input type="text" name="father-last_name" id="id_father-last_name" /></li>
In your case, you can make a new form for each X of your list. Put all new forms in a list named formset. Then, in your template, you should write form loop:
{{for form in formset}}
... Here your code for a form ....