Hi I have on my html file this, tags and tagInfos are both queryset´s of Django, that 2 for´s are only to show wich one belongs to the other:
<form action="/chart/chart/" method="get">
{% if tags.count > 0 %}
{% for tag in tags %}
{% for tagInfo in tagInfos %}
{% if tag.taginfo_idtaginfo1_id == tagInfo.idtaginfo %}
<p>
<input type="checkbox" name="checks[]" value="{{ tag.idtag }}" />
</p>
{% endif %}
{% endfor %}
{% endfor %}
{% csrf_token %}
<button type="submit" class="btn btn-primary">Submit creation</button>
{% else %}
<p>None TAG to select.</p>
{% endif %}
</form>
So and on my view i try to do that:
def chart(request):
if 'checks' in request.GET and request.GET['checks']:
chosen = request.GET.getlist('checks')
return render_to_response('Chart/chart.html',{'chosen':chosen})
else:
return render_to_response('Chart/chart.html',{})
But don´t show any of the selected checkboxes on the other html, I´m using {{ chosen }} to show.
Any ideas of what I´m doing wrong?
try this:
def chart(request):
if 'checks[]' in request.GET: #<----- 'checks[]'
chosen = request.GET.getlist('checks[]') #<----- 'checks[]'
return render_to_response('Chart/chart.html',{'chosen':chosen})
else:
return render_to_response('Chart/chart.html',{})
Related
inside my app I'm using an inlineformset_factory and right know it's working fine! But when displaying it inside my template the labels are always staying right above the input-field. Is there any way to display them side-by-side or even move the label as some sort of placeholder inside the input-field?
views.py
formset = inlineformset_factory(Model_A, Model_B, can_delete=False, extra=0, fields=('fields_01', 'fields_02', 'fields_03'))
template.html
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<button type="submit">Save</button>
</form>
Thanks for all your help and have a great weekend!
Try this:
<form action="" method="POST">
{% csrf_token %}
{{ form.management_form }}
{% for field in form %}
{{field.product.label}} - {{field.product}} #here product is my field name
<hr>
{% endfor %}
<input type="submit" name="Submit">
</form>
You have to do this for every field
#Changes
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form.Field_name.label }} - {{form.Field_name}} #1st field
{{ form.Field_name.label }} - {{form.Field_name}} #2nd field
{% endfor %}
<button type="submit">Save</button>
</form>
I think the displaying was influenced by css。
Use F12 to check css in chrome to find the reason。
You can edit the css to change the displaying.
Or use django-widgets-improved and bootstrap to set class to the fields of form.
{% load widget_tweaks %}
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{% render_field field class="form-control" %}
{% endfor %}
{% endfor %}
<button type="submit">Save</button>
</form>
I want registration and log in form in same page and in same html file. when i render form1 i have problem. i search in google but can't fix. can i render two form same html?
this is code --->
{% extends 'base.html' %}
<link rel="stylesheet" type="text/css" href="/static/css/style.css?{% now "U" %}" />
{% block content %}
<form action = '' method="post" novalidate="novalidate" id="reg" >
{% csrf_token %}
<div class="continer">
{% for field in form %}
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
{% endfor %}
<button type="submit" name="register" id="btn" >registraton</button>
<button type="button" name="log" id="lgn">login</button>
</div>
</form>
<form action="" method="post" novalidate="novalidate" id="log">
{% csrf_token %}
<div class="continer2">
{% for field in form1 %}
{{ field }}
</div>
</form>
{% endblock %}
You're missing an endfor for the last for loop:
{% for field in form1 %}
{{ field }}
{% endfor %}
The error message indicated that you might be missing an endwith or endfor which is a hint to look for a with or for that hasn't been closed.
I haven't found similar questions on stackoverflow, I'd like to change the save button to submit or confirm on the edit form. I know this might not be easily changed. Thanks for any advise in advance.
After search in the code of flask-admin, I found the button is rendered with macro render_form, render_form_buttons, extra. The value of these buttons is hard code with {{ _gettext("blabla") }}.
As these buttons are not fields of data model, we can't use rendering rules to custom the value. I think there are two work arounds to get this done:
change the macro which render these buttons in the source of flask-admin(render_form_buttons, extra)
flask-admin use flask-babelex to do localization({{ _gettext("blabla") }}), you can 'translate' Save to submit or confirm with flask-babelex
UPDATE:
You can custom edit.html in your own template directory.
{% extends 'admin/model/edit.html' %}
{% from 'admin/lib.html' import extra with context %}
{% from 'admin/lib.html' import form_tag with context %}
{% from 'admin/lib.html' import render_form_fields with context %}
{% macro my_render_form_buttons(cancel_url, extra=None, is_modal=False) %}
<hr>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 submit-row">
<input type="submit" class="btn btn-primary" value="{{ _gettext('Submit') }}" />
{% if extra %}
{{ extra }}
{% endif %}
{% if cancel_url %}
<a href="{{ cancel_url }}" class="btn btn-danger" role="button" {% if is_modal %}data-dismiss="modal"{% endif %}>{{ _gettext('Cancel') }}</a>
{% endif %}
</div>
</div>
{% endmacro %}
{% macro my_render_from(form, cancel_url, extra=None, form_opts=None, action=None, is_modal=False) -%}
{% call form_tag(action=action) %}
{{ render_form_fields(form, form_opts=form_opts) }}
{{ my_render_form_buttons(cancel_url, extra, is_modal) }}
{% endcall %}
{% endmacro %}
{% block edit_form %}
{{ my_render_form(form, return_url, extra(), form_opts) }}
{% endblock %}
I am creating form using Django Form wizard and formsets.
def get_form(self, step=None, data=None, files=None):
initial_data_set = []
for x in some_list:
initial_data_set.append({
'title' : x.title,
})
data = {
'form-TOTAL_FORMS': '5',
'form-INITIAL_FORMS': '5',
'form-MAX_NUM_FORMS': '',
}
formset_class = formset_factory(TitleForm, extra =0)
formset = formset_class(data=data, initial=initial_data_set)
return formset
Template
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block content %}
{% if wizard.form.forms %}
{% for form in wizard.form.forms %}
{{ form.media }}
{% endfor %}
{% else %}
{{ wizard.form.media }}
{% endif %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{% if ingestable_upload %}
<tr>{{ form.as_inline_table }}</tr>
{% else %}
{{ form.as_table }}
{% endif %}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "Submit" %}"/>
</form>
{% endblock %}
I was able to see initial data in my form when I was not passing 'data'. However, not passing data was giving me formset.is_valid as False and there was None in the cleaned_data. So I created data{} and passed it as per the documentation here -
https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#understanding-the-managementform
Since I started passing data, form is not getting populated with initial data.
I have put debug statements in formsets.py under BaseFormSet() class. It is getting both data and initial data that I am passing.
I have been struggling with this for few days. Any help on how I can populate my form and get cleaned data will be great.
What you're doing is overriding your form initial data with your data object, data attribute is not necessary that data is passed on the {{ formset.management_form }} in your template.
There's something else wrong with your form. I would suggest you print the errors when is_valid fails and see what's missing.
I have the following code in form.py
OPTIONS = ['Option1', 'Option2', 'Option3']
class Test_Form(Form):
test = SelectField('Dropdown', coerce= str,
choices=[(f, f) for f in OPTIONS])
submit = SubmitField('Submit')
And the following code in my template
<div class = "control-group">
<label class="control-label">
{{ form.test.label }} </label>
{% if form.test.errors %}
{% for error in form.test.errors %}
<p class="error-message"> {{ error }}</p>
{% endfor %}
{% endif %}
<div class="controls">
<select name=form.test.label width="80px">
{% for i,j in form.test.choices %}
<option value = {{ i }} > {{ j }} </option>
{% endfor %}
</select>
</div>
</div>
Following is my view function
def show_logs():
my_form = Test_Form()
if request.method == "POST":
if logs_form.validate() == False:
return render_template('test.html', form = my_form)
else:
return my_form.test.data
#return render_template('test_success.html', output = output_list)
elif request.method == "GET":
return render_template('test.html', form = my_form)
I get a "Not a Valid Choice" every time I submit the form. I went through the previous questions on SO and tried coerce = str but I still get the same message. What am I missing here?
I tried your code and it works perfectly on my end. The only thing I changed however was your template code as yours was incomplete. It had no submit button and no <form> tag declaration.
This is the template code that I used:
<form action="" method='post'>
{{ form.hidden_tag() }}
<ul class="request_form">
{%if form.errors%}
Please correct the following fields:
{%for each in form.errors%}
<br>{{each}}
{%endfor%}
{%endif%}
{%for each in form%}
{%if each.name != "csrf_token" and each.name!="submit"%}
<li>
<label>{{each.name}}</label>
{{each()}}
</li>
{%endif%}
{%endfor%}
<li/>{{form.submit}}
</ul>
</form>
Also, in your view, you're checking for logs_form.validate() when it should be my_form.validate()
I hope this solves your problem.