Thanks for the nice code ,
In the code while using inline formset you are giving extra = 1,so that we will get 1 formset for the image which contains image,alttext,delete i need a formset in such a way that crete a button like add image so that when you click on it whole div has to repeat and whatever data entered in the div has to save in the database
here is my code for reference
template_name = "books/publisher_books_update.html"
{% extends 'books/base.html' %}
{% block page_title %}Editing books for {{ publisher.name }}{% endblock %}
{% block body_title %}Editing books for
{{ publisher.name }}{% endblock %}
{% block content %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field.errors }}
{{ hidden_field }}
{% endfor %}
{% csrf_token %}
{{ form.management_form }}
{{ form.non_form_errors }}
{% for book_form in form.forms %}
<hr>
<h2>
{% if book_form.instance.id %}
Book #{{ book_form.instance.id }}</h2>
{% else %}
{% if form.forms|length > 1 %}
Add another book
{% else %}
Add a book
{% endif %}
{% endif %}
</h2>
{% for hidden_field in book_form.hidden_fields %}
{{ hidden_field.errors }}
{% endfor %}
<table>
{{ book_form.as_table }}
</table>
{% if book_form.nested %}
<div class="indent">
<h3>Images</h3>
<div class="form-list">
<div class="repeat"
style="border:3px dashed grey;padding-left: 11px;padding-right: 11px; margin-bottom:18px;">
<div class="rep">
<button type="button" onclick="addFormElements(this)"
class="btn btn-primary repeat-btn">Add image
</button>
<button type="button" onclick="removeFormElements(this)"
{# class="btn btn-primary remove-btn">Remove Image#}
{# #}
{{ book_form.nested.management_form }}
{{ book_form.nested.non_form_errors }}
{% for bookimage_form in book_form.nested.forms %}
<tr>
<td>
<em>
{% if bookimage_form.instance.id %}
BookImage #{{ bookimage_form.instance.id }}
{% else %}
Add an image
{% endif %}
</em>
</td>
<td>
{% for hidden_field in bookimage_form.hidden_fields %}
{{ hidden_field.errors }}
{% endfor %}
<table>
{{ bookimage_form.as_table }}
</table>
</td>
<td>
{% if bookimage_form.instance.image %}
<img src="{{ bookimage_form.instance.image.url }}" height="100"
alt="{{ nested.form.instance.alt_text }}">
{% endif %}
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
<hr>
<p>
<input type="submit" value="Update books">
<a href="{{ publisher.get_absolute_url }}">Cancel</a>
</p>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function addFormElements(current) {
$(current).parents('.form-list').append($(current).parents('.repeat').clone())
}
{#function removeFormElements(current) {#}
{# $(current).parents('.repeat').remove();#}
{#}#}
</script>
{% endblock content %}
Issue:
i tried above code and submit but second formset(book) is not saving in the database ,it will save only the latest records what i can do for that
Thank you,
Thanks for the nice code ,
In the code while using inline formset you are giving extra = 1,so that we will get 1 formset for the image which contains image,alttext,delete i need a formset in such a way that crete a button like add image so that when you click on it whole div has to repeat and whatever data entered in the div has to save in the database
here is my code for reference
template_name = "books/publisher_books_update.html"
{% extends 'books/base.html' %}
{% block page_title %}Editing books for {{ publisher.name }}{% endblock %}
{% block body_title %}Editing books for
{{ publisher.name }}{% endblock %}
{% block content %}
{# class="btn btn-primary remove-btn">Remove Image#}
{# #}
{{ book_form.nested.management_form }}
{{ book_form.nested.non_form_errors }}
{% for bookimage_form in book_form.nested.forms %}
{% endblock content %}
Issue:
i tried above code and submit but second formset(book) is not saving in the database ,it will save only the latest records what i can do for that
Thank you,