I need to expand a formset's field, so instead of
{{ form.name }}
I'm using something like
<input type="text" name="{{ form.name }}" ... >
My custom implementation does not print the formset prefix, so the HTML I get is
<input type="text" name="name" ... >
But what I would need to have the form working properly with formset is
<input type="text" name="attachments-3-name" ... >
where attachments-x is automatically added.
How can I get that?
I noted there's an helper for ID (auto_id) which prints something similar: id_attachments-3-name; is there something similar for names?
Looks like {{ form.name.html_name }} is what I've been looking for the last 2 hrs.
Related
i want to create two same name submit in html like this
<input type="submit" name="key" value="up">
<input type="submit" name="key" value="down">
but i want to use flask-wtf to do it, i don't know how to create Class?is like this?
class NameForm(FlaskForm):
submit = SubmitField('up')
submit = SubmitField('down')
No. Doing it like that will simply overwrite the class attribute submit. Do it like this:
class NameForm(FlaskForm):
key = SubmitField('not_used_string')
Then in your html after return render_template('page.html', form=form) you render it like this:
{{ form.key(value='up', id="A1") }} # -> will render <input id="A1" name="key" type="submit" value="up">
{{ form.key(value='down', id="A2") }} # -> will render <input id="A2" name="key" type="submit" value="down">
You don't have to supply id's but if you don't they will both be key.
Note that in order to have the same name you can only have one class attribute with that name.
I would like to initialize my form with a default empty value.
In my views.py i have one like so:
form = BookingForm(initial={'name':''})
and in the template like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value={{ form.name }}/></td></tr>
The output however, in the html input field shows '/' instead of the placeholder.
Any ideas how to do this?
Try adding quotes around your {{ form.name }} like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value="{{ form.name }}"/></td></tr>
and if that doesn't change anything you can try in your forms.py:
name = forms.CharField(initial='')
rather than trying to set the default input value upon instantiation.
I'm finding it overly difficult to customize ClearableFileInput as set as the default widget in a modelForm that includes an ImageField in the model.
Particularly I don't want the Delete Checkbox that is part of the widget. I've tried customizing/overriding the rendering in a number of ways to get rid of the checkbox including setting the widget to FileInput and overriding the render method where subclassing the widget in a widgets.py file.
The simplest I can explain the problem is like this:
forms.py
class SpecImageForm(ModelForm):
orig_image = forms.ImageField(required=False, widget=forms.FileInput)
class Meta:
model = SpecImage
fields = ['orig_image',]
# The intention is to have more than one SpecImageForm once this is working but for now the
# max_num is set to 1
SpecImageFormSet = inlineformset_factory(Spec, SpecImage, form=SpecImageForm, extra=1, max_num=1)
Despite explicitly setting the FileInput against the widget it renders like this in my template - still including the checkbox which I don't think should be present using FileInput.
<fieldset>
<legend>Images</legend>
<input id="id_specimage_set-TOTAL_FORMS" name="specimage_set-TOTAL_FORMS" type="hidden" value="1" />
<input id="id_specimage_set-INITIAL_FORMS" name="specimage_set-INITIAL_FORMS" type="hidden" value="0" />
<input id="id_specimage_set-MAX_NUM_FORMS" name="specimage_set-MAX_NUM_FORMS" type="hidden" value="1" />
<ul>
<li>
<label for="id_specimage_set-0-orig_image">Orig image:</label>
<input id="id_specimage_set-0-orig_image" name="specimage_set-0-orig_image" type="file" />
</li>
<li>
<label for="id_specimage_set-0-DELETE">Delete:</label>
<input id="id_specimage_set-0-DELETE" name="specimage_set-0-DELETE" type="checkbox" />
<input id="id_specimage_set-0-id" name="specimage_set-0-id" type="hidden" />
<input id="id_specimage_set-0-car" name="specimage_set-0-car" type="hidden" />
</li>
</ul>
</fieldset>
The relevant part of the template is this:
<fieldset>
<legend>Images</legend>
{{ image_form.management_form }}
{% for form in image_form %}
<ul>
{{ form.as_ul }}
</ul>
{% endfor %}
</fieldset>
The only thing slightly different that I'm doing is using an inlineformset_factory.
I've also tried to override the rendering of a widget using widgets.py but similarly seem unable to rid myself of the defualt settings - principally based on this thread.
Any ideas or solution to rid myself of the checkbox would be gratefully received!
I think this is to do with the inlineformset_factory applying a default can_delete parameter set to true, which was present regardless of how I'd prepared the form to use with it. Simply passing can_delete=False got rid of the Delete checkbox.
SpecImageFormSet = inlineformset_factory(Spec, SpecImage, form=SpecImageForm, extra=1, max_num=1, can_delete=False)
In addition when I rendered the form on it's own (without using inlineformset_factory) there was no sign of a 'Delete checkbox'. Then I found this SO post that explained why.
Getting there.
django form
{% for field in form.fields %}
{{field}}
</div>
{% endfor %}
If there is an error {{field.email}} will output this html with a post data value
<input id="id_email" type="text" value="gffdg" />
I want to use HTML 5 inputs but don't know how to get the post value if there is error
<input id="id_email" type="email" >
edit..
I was hoping not to use widgets with my django forms and just type the html5 code in my template (type="email" not type="text")
<input id="id_email" type="email" >`
but can't figure out how to get the value back after a post with errors.
<input value="?????" />
If you are trying to get the bound value of the email field, then the following template code should work:
<input id="id_email" type="email" value="{{ form.email.value }}">
If the value is not set, then it will be a blank field.
I have a form that is supposed to create a new 'Quote' record in Django. A 'Quote' requires a BookID for a foreign key.
This is my form
<form method="POST" action="{% url 'quotes:createQuote' %}">
{% csrf_token %}
<section>
<label for="q_text">Quote Text</label>
<input type="text" name="text" id="q_text" placeholder="Enter a Quote" style="padding-left:3px"> <br>
<label for="q_book">Book ID</label>
<input type="text" name="bookID" id="q_book" placeholder="Enter Book ID" style="padding-left:3px"> <br>
<label for="q_disp">Display Quote Now?</label>
<input type="radio" name="display" id="q_disp" value="True"> True
<input type="radio" name="display" value ="False">False <br>
<button value="submit">Submit</button>
</section>
</form>
And this is the method that it is targeting
def createQuote(request):
#b = get_object_or_404(Book, pk=request.bookID)
return HttpResponseRedirect(reverse('quotes:index'))
Somewhere in that request argument I assume there is some sort of field that contains the bookID the user will pass in on the form. How do I get at that information?
Bonus points for anyone who can tell me some way I can visualise data like I might with console.log(some.collection) in Javascript
if request.method == "POST":
book_id = request.POST['book_id']
Assuming you're sure it's in there. Otherwise you'll need to verify/provide a default value like you would for a normal python dictionary.
As for visualising the data, do you mean printing it to the console? In which case if you're running the django runserver you can just do print some_data. If you want it formatted a little nicer, you can use pretty print:
import pprint
pp = pprint.PrettyPrinter()
pp.pprint(some_data)