How to create a django form field with html - django

at the moment the html page looks something like this:
<form action="{% url 'sequence_details' %}" method="post">
{% csrf_token %}
<div class="container pb-3">
<table class="table">
{{ form.as_table }}
<tr><th><input type="text" id="extrafield"></th></tr>
</table>
</div>
<div class="container">
<input type="submit" class="btn btn-primary" value="Download">
</div>
</form>
extrafield appears as a text field but any text entered into it does not get sent to the backend, i.e. the result of form.cleaned_data.get("extrafield") is always None, no matter what was entered. I would like to be able to customise extrafield and write my own html for it, which is why I'm not just adding it to the form object. How can I get the result of extrafield's input to show up in form.cleaned_data?

you can get that by this in your .views function:
extrafield = request.POST.get('extrafield')
before that you need to give a name to your input:
<tr><th><input type="text" name="extrafield" id="extrafield"></th></tr>

Related

checkbox returns empty list django templates

I'm fairly new to django.What I am trying to do is values from selected checkboxes from my django template. But it gives an empty list in views.py. I have searched for solutions but so far I have been unsuccessfull.
my template:
{% for x in project_list %}
<tr >
<td >
<label>
<input type="checkbox" name="checks[]" value="{{x.id}}"/>
<span><a href= '{% url "projects:ProjectDetail" Pid=x.id %}' >{{x.project_name}}</a><br></span>
</label>
</td>
</tr>
{%endfor%}
<a class="waves-effect waves-light btn-small" id="nxtBtn" href="{% url 'projects:ProjectDelete' %}">Delete selected projects</a>
views.py
def Delete_project(request,*args, **kwargs):
some_var = request.POST.getlist('checks[]')
print("printing project Id's")
print(some_var)
what I want to do is when I click on Delete selected projects I should get list of seleted checkbox values.But I'm getting an empty list when I print list in views.
Thanks in advance!
You don't seem to have a form. A link will not submit any data anywhere. You need an actual HTML form with an action attribute, and a submit button:
<form action="{% url 'projects:ProjectDelete' %}" method="POST">
{% for x in project_list %}
...
{% endfor %}
<button type="submit" class="waves-effect waves-light btn-small" id="nxtBtn">Delete selected projects</button>
</form>

Sending data in django without creating forms

There is already a subscribe field in my html page.
I don't want to create another django form.
how can I send data from here?
<div class="input-group">
<span class="input-group-addon fh5co_footer_text_box" id="basic-addon1"><i class="fa fa-envelope"></i></span>
<input type="text" class="form-control fh5co_footer_text_box" placeholder="Enter your email..." aria-describedby="basic-addon1">
<i class="fa fa-paper-plane-o"></i> Subscribe
</div>
You can put your code inside form tags that points to your django view:
<form action="url_of_your_view" method="post">
{% csrf_token %}
</form>
and you can get the data in your view using request.POST but don't forget the {% csrf_token %} in your form.

django: fill checkbox based on url params

i am having a form with only checkboxes:
<div class="container">
<form method="GET" class="form-inline" action="">
<div>
{% for temp in instance.menu_positions_tags.all %}
<label class="checkbox-inline">
<input type="checkbox" name="tags[]" value="{{ temp.name}}">{{ temp.name }}
</label>
{% endfor %}
</div>
<div style="margin-top:10px">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
When i select few checkboxes and submit the url changes to localhost/test/?tags[]=day4&tags[]=day2. After the page loads the all checkboxes are unselected.
Now, how to, in my form make day4 and day2 checkboxes checked.
If you have checkboxes in your form file you can just render it from the form when you receive the GET request. And I think your form type should be POST.

How to output django_filters.RangeFilter text boxes inline?

How to output django_filters.RangeFilter textboxes inline?
I use django_filters and django-bootstrap-form
My template.html
<div class="row">
<div class="col-md-12">
<form action="" method="get" class="form-horizontal">
{{ filter.form|bootstrap_horizontal }}
<input type="submit" class="btn btn-info" />
</form>
</div>
</div>
UPDATE
I have several text filters in filterset, and I want to place in horizontal, and only RangeFilter are inline.
Try this:
{{ filter.form|bootstrap_inline }}
OR
<form action="" method="get" class="form-inline">
To use class="form-inline" on the form element, also change the "|boostrap" template tag to "|bootstrap_inline".

django model form with bootstrap doesn't work

I am making a website using django but I have a problem with my modelforms. This is my working form code in my html-file:
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
{% csrf_token %}
{{ form.as_p }}
</div>
<input type="submit" value="Inschrijven!" class="btn btn-primary" />
</form>
If i use this code, everything works fine, when I fill in the form and press send, I can see the information from my admin-page.
I now tried to style my form using bootstrap:
<form class="form-horizontal" role="form" method="post" action="">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<label class="col-sm-4 control-label">{{ field.label }}</label>
<div class="col-sm-4">
<input type="text" class="form-control" placeholder="{{field.label}} ingeven">
</div>
</div>
{% endfor %}
<input type="submit" value="Inschrijven!" class="btn btn-primary">
</form>
Now I have a nice bootstrap layout but if I fill in the form, I can't see the information at my admin-page.
Does somebody know how I can fix this so I can keep the nice layout and still have my form to work?
When you output Django HTML form, you will notice that it creates input elements with the following ID and Name attributes of the element.
Eg
class MyForm...
name = ...
age = ...
HTML
<input type='text' id='id_name' name='name' ...
<input type='text' id='id_age' name='age'...
To fix your issue, in your HTML add the following attribute to the HTML input field you're generating
id="id_{{field.name}}" name="{{field.name}}"
Reasoning:
When the form is submitted, the browser will send form encoded data in the form of name=value pair, which then arrives in form of dict in request.POST and when fed into Django form, it pre fills the form by matching the Django form attribute name to POST values.