0

I have a form in which I have a table.In table I have a record(title).Now along with title I have two buttons delete and edit.Now I want is that, if I click edit it calls separate url along with primary key going through it or if I click delete It calls separate url with primary key going through.

here's my code:

       <form method="POST" action="{% url 'essay:delete_view' teacher.id %}">
     <div class="page-header">
      <div class="span4">
        <h1>Manage Essays</h1>
      </div>
  </div>
    <table>
        <tr>
            <th>Title</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
      {% csrf_token %}
        {% for uploadassignment in teacher.uploadassignment_set.all %}
         <tr>
             <th for="uploadassignment{{ forloop.counter }}">{{uploadassignment.id}}</th>
             <th><input  type="submit"  id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
             <th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment"  class="btn btn-primary"/></th>
         </tr>
        {% endfor %}
    </table>
        </form>

I want this: one url is called by below (Edit button) with teacher.id

 <th><input  type="submit"  id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>

And another url is called when hit delete button with teacher id.

    <th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment"  class="btn btn-primary"/></th>

So far due to action ="{% url 'essay:delete_view' teacher.id %}" in form I can switch to only one url.Kindly help in my code so I can switch to different urls with teacher id when clicked on delete or edit button.

urls.py:

          url(r'^$', views.Indexview.as_view(), name='index'),
url(r'^login/$',views.loginform.as_view(),name='login'),
url(r'^addfile/$',views.Addfile.as_view(),name='file'),    #essay/addfile
url(r'^addfile/$',views.EditEssay.as_view(),name='eEssay'),
url(r'^text/$',views.writetext.as_view(),name='text'),
url(r'^about/$',views.aboutus.as_view(),name='about'),
url(r'^stdprofile/$',views.studentprofile.as_view(),name='stdprofile'),
url(r'^logout/$', LogoutView.as_view(), name='user_logout'),
url(r'^(?P<teacher_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view'),
Nabeel Ayub
  • 1,060
  • 3
  • 15
  • 35
  • 1
    You can't post to different URLs (well, not without using client side Javascript), but you can detect which submit button was used in your view code. See [this question](https://stackoverflow.com/questions/866272/how-can-i-build-multiple-submit-buttons-django-form). – Selcuk Oct 19 '18 at 05:32
  • could you post your urls – Pankaj Sharma Oct 19 '18 at 07:49
  • Urls added.Can you please tell me another way how I call different urls from two different button if my above approach doesn't work... – Nabeel Ayub Oct 19 '18 at 09:41
  • Use multiple form per different button – seuling Oct 19 '18 at 13:39

0 Answers0