Commit a7eaad9e authored by Washington99's avatar Washington99

Implemented the add author button

The add author button now directs to the forms where new entries may be added to the database. However, the form itself still doesn't have the needed functionality.
parent 6b0f96d2
......@@ -8,6 +8,11 @@ Created on Tue Apr 25 13:45:10 2023
from .models import Author, Books
from django import forms
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = "__all__"
class BookForm(forms.ModelForm):
class Meta:
model = Books
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Author</title>
</head>
<body>
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Add Author">
</form>
</body>
</html>
\ No newline at end of file
......@@ -22,7 +22,7 @@
<a href="/books">Books</a>
</div>
<div>
<a href="">Add Author</a>
<a href="/authors/add">Add Author</a>
<a href="/books/add">Add Book</a>
</div>
......
from django.urls import path
from .views import home_view, AuthorList, BookList, AuthorView, BookView
from .views import BooksCreateView
from .views import AuthorCreateView, BooksCreateView
urlpatterns = [
path('home/', home_view, name = 'home_view'),
......@@ -8,6 +8,7 @@ urlpatterns = [
path('books/', BookList.as_view(), name = 'books'),
path('authors/<int:pk>/details/', AuthorView.as_view(), name = 'authors_details'),
path('books/<int:pk>/details/', BookView.as_view(), name = 'books_details'),
path('authors/add/', AuthorCreateView.as_view(), name = 'add-author'),
path('books/add/', BooksCreateView.as_view(), name = 'add-book'),
path('books/<int:pk>/edit/', BooksCreateView.as_view(), name = 'edit-book')
]
......
......@@ -5,7 +5,7 @@ from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books
from .forms import BookForm
from .forms import AuthorForm, BookForm
# Create your views here.
......@@ -27,6 +27,12 @@ class AuthorView(DetailView):
class BookView(DetailView):
model = Books
template_name = 'bookshelf/book_details.html'
class AuthorCreateView(CreateView):
model = Author
form = AuthorForm # Can be removed?
fields = '__all__'
template_name = 'bookshelf/add-author.html'
class BooksCreateView(CreateView):
model = Books
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment