Pojawia się błąd:
NoReverseMatch at /books/edit/
Reverse for 'books_list' with keyword arguments '{'error': 'Invalid book code!'}' not found. 1 pattern(s) tried: ['books/$']
Występuje on w 20 linijce kodu przy poleceniu
return redirect('books_list', error="Invalid book code!")
w pliku views.py
from django.shortcuts import render
from django.shortcuts import redirect
from django.utils import timezone
import datetime
from random import random
from .models import book
from .forms import AddBook, EditBook
# Create your views here.
def books_list(request, error=""):
return render(request, "books_list.html", {'books': book.objects.all(), 'error': error})
def edit_book(request):
if request.method == "POST":
edited_book=book.objects.filter(Unique_Code=request.POST.get("edit"))
if not edited_book.exists():
return redirect('books_list', error="Invalid book code!")
return render(request, "edit_book.html", {'book': edited_book})
def add_book(request):
if request.method == "POST":
form=AddBook(request.POST)
if form.is_valid():
Added_book = form.save(commit=False)
Added_book.Date_of_Add = timezone.now()
Added_book.Unique_Code = random.randint(10000, 99999)
Added_book.save()
if 'save_button' in request.POST:
return redirect('books_list')
elif 'save+next_button':
return redirect('add_book')
else:
form=AddBook()
return render(request, "add_book.html", {'form': form})
Tu daje jeszcze kilka plików jakby coś:
urls.py
from django.contrib import admin
from django.urls import path
from books_manage.views import books_list, add_book, edit_book
from students_manage.views import students_list, add_student, search_student
urlpatterns = [
path('books/', books_list, name='books_list'),
path('books/add/', add_book, name='add_book'),
path('books/edit/', edit_book, name='edit_book'),
path('students/', students_list, name='students_list'),
path('students/add/', add_student),
path('students/search/', search_student),
path('admin/', admin.site.urls),
]
books_list.html
<h2 align="center">Books List</h2>
<p align="Right">
<a href="/students"><button>Go to the students list</button></a>
</p> <br><br>
<p>
<a href="{% url 'add_book' %}" align="Left"><button>Add book</button></a>
<form method="POST" align="Right" action="{% url 'edit_book' %}"> {%csrf_token%}
<font color="FF0000" size="3">{{error}}</font><br>
<input type="text" placeholder="Type book code to edit..." name="edit">
<button type="submit">Edit</button>
</form>
</p>
<table border="2">
<tr>
<td width="20%">Title</td>
<td width="15%">Author</td>
<td width="4%">Issue Year</td>
<td width="8%">Publisher</td>
<td width="3%">Unique Code</td>
<td width="5%">Borrower ID</td>
</tr>
{% for books in books %}
<tr>
<td>{{books.Title}}</td>
<td>{{books.Author}}</td>
<td>{{books.Issue_Year}}</td>
<td>{{books.Publisher}}</td>
<td>{{books.Unique_Code}}</td>
<td>{{books.Borrower_Id}}</td>
</tr>
{% endfor %}
</table>
edit_book.html
<h2>Edit book</h2>
<form method="POST"> {%csrf_token%}
{% for book in book %}
<input type="text" name="Title" value="{{book.Title}}"><br>
<input type="text" name="Title" value="{{book.Author}}"><br>
<input type="text" name="Title" value="{{book.Issue_Year}}"><br>
<input type="text" name="Title" value="{{book.Publisher}}"><br>
<input type="text" name="Title" value="{{book.Borrower}}"><br>
{% endfor %}
<button type="submit" value="save" name="save_button">Save</button>
</form>
<a href="/books/"><button>Go back to the book list</button></a>