'Reverse for `view` with arguments '('',)' not found. 1 pattern(s) tried: `[u'bms/update/(?P<id>[0-9]+)/$']`

This is my update view which I'm using to get pre-populated form. Although this is also not working.

def update(request, id):
    item = get_object_or_404(BookEntry, id=id)
    if request.method=="POST":
        form = UpdateForm(request.POST, instance=item)
        if form.is_valid():
            post=form.save(commit=False)
            post.save()
            return HttpResponseRedirect(reverse('bms:index'), id)
        else:
            form=EntryForm(instance=item)
            return HttpResponseRedirect(reverse('bms:index'), id)
        return render(request, 'index.html', {'form':form})

This is my urls.py

url(r'^update/(?P<id>[0-9]+)/$', views.update, name='update'),

This is the error that I'm getting again and again:

NoReverseMatch at /bms/ Reverse for 'update' with arguments '('',)' not found. 1 pattern(s) tried: [u'bms/update/(?P[0-9]+)/$']

I'm not sure if passing the url in right way. This is what i'm doing:

<form action="{% url 'bms:update' book.id %}" id="updateform" name="updateform" method="POST">

When I look at traceback, it's showing something's wrong in above line and this line:

return render(request, "bms/index.html", context) 

This is my index view:

def index(request):
    context = {}
    book_detail = BookEntry.objects.all()
    context.update({
        'book_detail': book_detail
    })
    response = {"status": False, "errors": []}
    if request.is_ajax():
        id = request.POST['id']
        csrf_token = request.POST['csrfmiddlewaretoken']
        response = {}
        response['status'] = False
        book = BookEntry.objects.filter(id=id).first()
        context = {
            "book": book,
            "csrf_token": csrf_token
        }
        template = render_to_string("bms/index.html", context)
        response['template'] = template
        response['status'] = True
        return HttpResponse(json.dumps(response), content_type="applicaton/json")
    return render(request, "bms/index.html", context)

My index.html:

    <!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>


function update_entry(id) {
  var id = id
  $.ajax({
    url: "{% url 'bms:index' %}",
    type: "POST",
    data:{
        'csrfmiddlewaretoken': '{{ csrf_token }}',
        'id' : id,
      },
    success: function(response){
        $("#updateform").modal("show");
      }
    });
}

function update_property(id) {
  window.location.replace("/bms/update" + id+"/");
}
</script>



<style>
  body{
    font-family: Raleway;
  }

</style>
</head>
<body>
  <div class="container">
    <div class="page-header">
      <h1>Details of Books</h1>
    </div>
    <table class = "table table-bordered table-hover">
      <tr>
        <th>S No.:</th>
        <th>Title:</th>
        <th>Author:</th>
        <th>Edition:</th>
        <th>Publisher:</th>
        <th>Genre:</th>
        <th>Detail:</th>
        <th>Language:</th>
        <th>Price:</th>
        <th>ISBN:</th>
        <th>Action:</th>
      </tr>
      {% for item in book_detail %}
      <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ item.title }}</td>
        <td>{{ item.author }}</td>
        <td>{{ item.edition }}</td>
        <td>{{ item.publisher }}</td>
        <td>{{ item.genre }}</td>
        <td>{{ item.detail }}</td>
        <td>{{ item.language }}</td>
        <td>{{ item.price }}</td>
        <td>{{ item.isbn }}</td>
        <td>
          <a data-href="bms:update({{item.id}})" onclick="update_entry({{item.id}})" class="btn btn-warning" data-toggle="modal">
            <span class = "glyphicon glyphicon-pencil"></span> Edit
          </a>
        </td>
      </tr>
      {% endfor %}
    </table>
    <div  class="modal fade" id="updateform" role="dialog">
      <div class="modal-dialog">
        <div class = "modal-content">
          <div class = "modal-header">
            <button type = "button" class = "close" data-dismiss="modal">&times;</button>
            <h3 class="modal-title">
              <b>Update Details</b>
            </h3>
          </div>
          <div class = "modal-body">
            <form action="{% url 'bms:update' book.id %}" id="updateform" name="updateform" method="POST">
              {% csrf_token%}
              <div class = "form-group">
                <label for = "title">
                  Title:
                </label>
                <input class = "form-control" id="book_title" type = "text" name="title" value="{{ book.title }}">
              </div>
              <div class="form-group">
                <label for = "author">
                  Author:
                </label>
                <input id="book_author" class = 'form-control' type = "text" name="author" value="{{ book.author }}">
              </div>
              <div class = "form-group">
                <label for = "edition">
                  Edition:
                </label>
                <input id="book_edition" type = "text" class = 'form-control' name="edition" value="{{ book.edition }}">
              </div>
              <div class = "form-group">
                <label for ="publisher">
                  Publisher:
                </label>
                <input id="book_publisher" type = "text" name="publisher" class = 'form-control' value="{{ book.publisher }}"/>
              </div>
              <div class = "form-group">
                <label for ="genre">
                  Genre:
                </label>
                <input id="book_genre" type = "text" name="genre" class = 'form-control' value="{{ book.genre }}"/>
              </div>
              <div class = "form-group">
                <label for ="language">
                  Language:
                </label>
                <input id="book_language" type = "text" name="language" class = 'form-control' value="{{ book.language }}"/>
              </div>
              <div class = "form-group">
                <label for ="price">
                  Price:
                </label>
                <input id="book_price" type = "text" name="price" class = 'form-control' value="{{ book.price }}"/>
              </div>
              <div class = "form-group">
                <label for ="isbn">
                  ISBN:
                </label>
                <input id="book_isbn" type = "text" name="isbn" class = 'form-control' value="{{ book.isbn }}"/>
              </div>
              <input type = "submit" value="Update" id="update" class = "btn btn-success" style="font-size:18px;" />
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>


Solution 1:[1]

Does the BookEntry model have a primary key column? I started having the same error when I added a primary key field on the model the id was referencing. Apparently the id column is replaced by the primary key field. I removed the key and migrated the changes.The error is gone now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AfriPwincess