Reputation: 171
Trying to display a Page to update record but receiving following error.
ERROR
NoReverseMatch at /edit-expense/2
Reverse for 'edit-expense' with no arguments not found. 1 pattern(s) tried: ['edit\\-expense/(?P<id>[0-9]+)$']
Request Method: GET
Request URL: http://127.0.0.1:8000/edit-expense/2
Django Version: 3.2.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'edit-expense' with no arguments not found. 1 pattern(s) tried: ['edit\\-expense/(?P<id>[0-9]+)$']
Exception Location: C:\Users\Ideation\AppData\Roaming\Python\Python39\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: C:\Program Files\Python39\python.exe
Python Version: 3.9.5
Python Path:
['C:\\xampp\\htdocs\\Projects\\Ideation\\Ideation',
'C:\\Users\\Ideation\\AppData\\Roaming\\Python\\Python39\\site-packages\\_pdbpp_path_hack',
'C:\\Program Files\\Python39\\python39.zip',
'C:\\Program Files\\Python39\\DLLs',
'C:\\Program Files\\Python39\\lib',
'C:\\Program Files\\Python39',
'C:\\Users\\Ideation\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\Ideation\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32',
'C:\\Users\\Ideation\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32\\lib',
'C:\\Users\\Ideation\\AppData\\Roaming\\Python\\Python39\\site-packages\\Pythonwin',
'C:\\Program Files\\Python39\\lib\\site-packages']
Server time: Fri, 27 Aug 2021 05:30:22 +0000
views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import Category, Expense
@login_required(login_url = "login")
def expense_edit(request, id):
expense = Expense.objects.get(pk=id)
context = {
'expense': expense,
'values': expense,
}
if request.method == 'GET':
return render(request, 'expenses/edit_expense.html', context)
# if request.method == "POST":
else:
messages.add_message(request, messages.INFO, 'Handling post form')
return render(request, 'expenses/edit_expense.html', context)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
path('add-expense', views.add_expense, name="add-expense"),
path('edit-expense/<int:id>', views.expense_edit, name="edit-expense"),
]
edit_expense.html
{% extends 'base.html' %}
{% block content %}
<div class="container mt-4">
<h2>Edit Expense</h2>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{% url 'index' %}">Expenses</a></li>
<li class="breadcrumb-item active" aria-current="page">Edit Expense</li>
</ol>
</nav>
<div class="card">
<div class="card-body">
<form action="{% url 'edit-expense' %}" method="POST">
{% csrf_token %}
{% include 'partials/_messages.html' %}
<div class="form-group">
<label for="amountvalue">Amount</label>
<input value="{{values.amount}}" type="number" class="form-control-sm form-control" name="amount"
id="amountvalue">
</div>
<div class="form-group">
<label for="descriptionvalue">Description</label>
<input value="{{values.description}}" type="text" class="form-control-sm form-control"
name="description" id="descriptionvalue">
</div>
<div class="form-group">
<label for="categoryvalue">Category</label>
<select name="category" id="categoryvalue" class="form-control-sm form-control">
{% for category in categories %}
<option value="{{category.name}}">{{category.name}}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="dateofexpensevalue">Date of Expense</label>
<input value="{{values.date}}" type="date" class="form-control-sm form-control" name="expense_date"
id="dateofexpensevalue">
</div>
<input type="submit" value="Update" class="btn btn-primary mt-2">
</form>
</div>
</div>
</div>
{% endblock content %}
index.html
{% extends 'base.html' %}
{% block content %}
<div class="container-fluid mt-4">
<div class="row">
<!-- <h2>Expenses List</h2> -->
<div class="col-md-10">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">Expenses</a></li>
<li class="breadcrumb-item active" aria-current="page">My Expenses</li>
</ol>
</nav>
</div>
<div class="col-md-2">
<a href="{% url 'add-expense' %}" class="btn btn-primary">Add Expense</a>
</div>
</div>
<br>
{% include 'partials/_messages.html' %}
{% if expenses.count %}
<!-- <div class="container-fluid"></div> -->
<table class="table table-stripped table-hover mt-4">
<thead>
<tr>
<th>Amount ($)</th>
<th>Category</th>
<th>Description</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
{% for expense in expenses %}
<tr>
<td>{{expense.amount}}</td>
<td>{{expense.category}}</td>
<td>{{expense.description}}</td>
<td>{{expense.date}}</td>
<td> <a href="{% url 'edit-expense' expense.id %}" class="btn btn-secondary btn-sm">Edit</a> </td>
</tr>
{% endfor %}
</tbody>
</table>
<!-- </div> -->
{% endif %}
</div>
{% endblock content %}
CODE EXPLANATION
- VIEWS
edit_expense.html
is required.edit_expense.html
, we are only displaying it.- URLS
- EDIT EXPENSE
block
template tag adding breadcrumb, after that a card which contains a HTML form._messages.html
is only to display different messages which is additinal and rest of things basic HTML and some bootstrap.INDEX
Upvotes: 1
Views: 3029
Reputation: 27513
your main problem is in this line in the edit-expense.html
<form action="{% url 'edit-expense' %}" method="POST">
as your view requires an ID to be passed to the URL.
you need to pass ID to the form action like this line
{% url 'edit-expense' expense.id %}
Upvotes: 5