How do I create a new form in inline formset by clicking a link












1















I created an inline relation between Reader and Book models.
I imported the formset in my template successfully but I can not create a new Book form by clicking a link related to my addtext attribute in the below script. In otherwords, for one Reader I want to be able to create more than one Book form by clicking a link.



   <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>


The whole template is this:



{% extends 'base.html' %}
{% load bootstrap3 %}
{% load static %}
<!-- Latest compiled and minified JavaScript -->


{% block content %}

<div class="col-md-12 text-center">
<h2>Create / Edit Reader </h2>
</div>

<hr>

<form class="well" method="post" action="">
{% csrf_token %}
{% bootstrap_form form %}

<table class="table book">
{{ formset.management_form }}

{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>

{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</form>
<hr>

{{ form.media }}
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/base.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}"/>
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.min.js"></script>
<script type="text/javascript" src="/static/admin/js/calendar.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/DateTimeShortcuts.js"></script>

{% endblock %}


Here is my view:



class ReaderBookCreateView(LoginRequiredMixin, CreateView):
model = Reader
fields = '__all__'
template_name='test/test.html'

def get_context_data(self, **kwargs):
data = super(ReaderBookCreateView, self).get_context_data(**kwargs)
if self.request.POST:
data['formset'] = BookFormSet(self.request.POST)#bound the formset with data
else:
data['formset'] = BookFormSet()#empty formset
return data

def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
with transaction.atomic():
self.object = form.save()

if formset.is_valid():
formset.instance = self.object
formset.save()
return super(ReaderBookCreateView, self).form_valid(form)

def get_success_url(self, **kwargs):
return reverse('client_list')


Here is how it looks. I would like to have a link Add new Book above the submit button for creation of the new Book form for the current reader.
enter image description here



Any help will be appreciated.










share|improve this question

























  • Have you considered using Javascript? I think there is a way this can be done without it.

    – Daniel H.
    Nov 23 '18 at 13:20


















1















I created an inline relation between Reader and Book models.
I imported the formset in my template successfully but I can not create a new Book form by clicking a link related to my addtext attribute in the below script. In otherwords, for one Reader I want to be able to create more than one Book form by clicking a link.



   <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>


The whole template is this:



{% extends 'base.html' %}
{% load bootstrap3 %}
{% load static %}
<!-- Latest compiled and minified JavaScript -->


{% block content %}

<div class="col-md-12 text-center">
<h2>Create / Edit Reader </h2>
</div>

<hr>

<form class="well" method="post" action="">
{% csrf_token %}
{% bootstrap_form form %}

<table class="table book">
{{ formset.management_form }}

{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>

{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</form>
<hr>

{{ form.media }}
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/base.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}"/>
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.min.js"></script>
<script type="text/javascript" src="/static/admin/js/calendar.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/DateTimeShortcuts.js"></script>

{% endblock %}


Here is my view:



class ReaderBookCreateView(LoginRequiredMixin, CreateView):
model = Reader
fields = '__all__'
template_name='test/test.html'

def get_context_data(self, **kwargs):
data = super(ReaderBookCreateView, self).get_context_data(**kwargs)
if self.request.POST:
data['formset'] = BookFormSet(self.request.POST)#bound the formset with data
else:
data['formset'] = BookFormSet()#empty formset
return data

def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
with transaction.atomic():
self.object = form.save()

if formset.is_valid():
formset.instance = self.object
formset.save()
return super(ReaderBookCreateView, self).form_valid(form)

def get_success_url(self, **kwargs):
return reverse('client_list')


Here is how it looks. I would like to have a link Add new Book above the submit button for creation of the new Book form for the current reader.
enter image description here



Any help will be appreciated.










share|improve this question

























  • Have you considered using Javascript? I think there is a way this can be done without it.

    – Daniel H.
    Nov 23 '18 at 13:20
















1












1








1








I created an inline relation between Reader and Book models.
I imported the formset in my template successfully but I can not create a new Book form by clicking a link related to my addtext attribute in the below script. In otherwords, for one Reader I want to be able to create more than one Book form by clicking a link.



   <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>


The whole template is this:



{% extends 'base.html' %}
{% load bootstrap3 %}
{% load static %}
<!-- Latest compiled and minified JavaScript -->


{% block content %}

<div class="col-md-12 text-center">
<h2>Create / Edit Reader </h2>
</div>

<hr>

<form class="well" method="post" action="">
{% csrf_token %}
{% bootstrap_form form %}

<table class="table book">
{{ formset.management_form }}

{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>

{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</form>
<hr>

{{ form.media }}
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/base.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}"/>
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.min.js"></script>
<script type="text/javascript" src="/static/admin/js/calendar.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/DateTimeShortcuts.js"></script>

{% endblock %}


Here is my view:



class ReaderBookCreateView(LoginRequiredMixin, CreateView):
model = Reader
fields = '__all__'
template_name='test/test.html'

def get_context_data(self, **kwargs):
data = super(ReaderBookCreateView, self).get_context_data(**kwargs)
if self.request.POST:
data['formset'] = BookFormSet(self.request.POST)#bound the formset with data
else:
data['formset'] = BookFormSet()#empty formset
return data

def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
with transaction.atomic():
self.object = form.save()

if formset.is_valid():
formset.instance = self.object
formset.save()
return super(ReaderBookCreateView, self).form_valid(form)

def get_success_url(self, **kwargs):
return reverse('client_list')


Here is how it looks. I would like to have a link Add new Book above the submit button for creation of the new Book form for the current reader.
enter image description here



Any help will be appreciated.










share|improve this question
















I created an inline relation between Reader and Book models.
I imported the formset in my template successfully but I can not create a new Book form by clicking a link related to my addtext attribute in the below script. In otherwords, for one Reader I want to be able to create more than one Book form by clicking a link.



   <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>


The whole template is this:



{% extends 'base.html' %}
{% load bootstrap3 %}
{% load static %}
<!-- Latest compiled and minified JavaScript -->


{% block content %}

<div class="col-md-12 text-center">
<h2>Create / Edit Reader </h2>
</div>

<hr>

<form class="well" method="post" action="">
{% csrf_token %}
{% bootstrap_form form %}

<table class="table book">
{{ formset.management_form }}

{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %} formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
$('table.book tr.formset_row').formset({
addText: 'Add new Book',
deleteText: 'Delete',
prefix: 'reader_book_set',
animateForms: true
});
</script>

{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
</form>
<hr>

{{ form.media }}
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/base.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}"/>
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.min.js"></script>
<script type="text/javascript" src="/static/admin/js/calendar.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/DateTimeShortcuts.js"></script>

{% endblock %}


Here is my view:



class ReaderBookCreateView(LoginRequiredMixin, CreateView):
model = Reader
fields = '__all__'
template_name='test/test.html'

def get_context_data(self, **kwargs):
data = super(ReaderBookCreateView, self).get_context_data(**kwargs)
if self.request.POST:
data['formset'] = BookFormSet(self.request.POST)#bound the formset with data
else:
data['formset'] = BookFormSet()#empty formset
return data

def form_valid(self, form):
context = self.get_context_data()
formset = context['formset']
with transaction.atomic():
self.object = form.save()

if formset.is_valid():
formset.instance = self.object
formset.save()
return super(ReaderBookCreateView, self).form_valid(form)

def get_success_url(self, **kwargs):
return reverse('client_list')


Here is how it looks. I would like to have a link Add new Book above the submit button for creation of the new Book form for the current reader.
enter image description here



Any help will be appreciated.







django django-forms inline formset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:39









Daniel H.

420314




420314










asked Nov 23 '18 at 10:55









gtopalgtopal

3010




3010













  • Have you considered using Javascript? I think there is a way this can be done without it.

    – Daniel H.
    Nov 23 '18 at 13:20





















  • Have you considered using Javascript? I think there is a way this can be done without it.

    – Daniel H.
    Nov 23 '18 at 13:20



















Have you considered using Javascript? I think there is a way this can be done without it.

– Daniel H.
Nov 23 '18 at 13:20







Have you considered using Javascript? I think there is a way this can be done without it.

– Daniel H.
Nov 23 '18 at 13:20














1 Answer
1






active

oldest

votes


















0














It was as usual in these situations a javascript issue. I solve it.
My project could not found the path for jquery.formset.js file.
Use F12 always when you trying to debug javascript.



enter image description here






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445350%2fhow-do-i-create-a-new-form-in-inline-formset-by-clicking-a-link%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It was as usual in these situations a javascript issue. I solve it.
    My project could not found the path for jquery.formset.js file.
    Use F12 always when you trying to debug javascript.



    enter image description here






    share|improve this answer




























      0














      It was as usual in these situations a javascript issue. I solve it.
      My project could not found the path for jquery.formset.js file.
      Use F12 always when you trying to debug javascript.



      enter image description here






      share|improve this answer


























        0












        0








        0







        It was as usual in these situations a javascript issue. I solve it.
        My project could not found the path for jquery.formset.js file.
        Use F12 always when you trying to debug javascript.



        enter image description here






        share|improve this answer













        It was as usual in these situations a javascript issue. I solve it.
        My project could not found the path for jquery.formset.js file.
        Use F12 always when you trying to debug javascript.



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 14:08









        gtopalgtopal

        3010




        3010
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445350%2fhow-do-i-create-a-new-form-in-inline-formset-by-clicking-a-link%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings