Muh*_*sim 1 python django django-templates django-views python-3.x
尝试显示页面以更新记录但收到以下错误。
错误
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
Run Code Online (Sandbox Code Playgroud)
视图.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)
Run Code Online (Sandbox Code Playgroud)
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"),
]
Run Code Online (Sandbox Code Playgroud)
编辑费用.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 %}
Run Code Online (Sandbox Code Playgroud)
索引.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 %}
Run Code Online (Sandbox Code Playgroud)
代码解释
- 观点
edit_expense.html需要登录。edit_expense.html,我们只是显示它。- 网址
- 编辑费用
block模板标签中添加面包屑,然后是包含 HTML 表单的卡片。_messages.html显示不同的消息,这是附加的,其余的都是基本的 HTML 和一些引导程序。指数
您的主要问题是在 edit-expense.html 中的这一行
<form action="{% url 'edit-expense' %}" method="POST">
Run Code Online (Sandbox Code Playgroud)
因为您的视图需要将 ID 传递到 URL。
您需要将 ID 传递给表单操作,如下行
{% url 'edit-expense' expense.id %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2931 次 |
| 最近记录: |