Commit b39811d6 authored by Ysobel Vera's avatar Ysobel Vera

edited views.py, created forms.py and edit-author and edit-book files

+edited views.py to have a post function and HttpResponseRedirect to properly redirect the urls of the new books and authors and their details
+created forms.py and put BookForm and AuthorForm classes so the forms can be saved after post
+created edit-author and edit-book files
parent a1966700
from django import forms
from .models import Author, Books
class BookForm(forms.ModelForm):
class Meta:
model = Books
fields = "__all__"
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = "__all__"
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}Add New Author{% endblock %}
{% block content %}
<form action="/" method="post">
{{ 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.as_p }}
<!--<label for="title">First Name:</label>
<input type="text" name="first_name" id="first_name"><br>
<label for="author">Last Name:</label>
<input type="text" name="last_name" id="last_name"><br>
<label for="age">Age:</label>
<input type="number" name="age" id="age"><br>
<label for="nationality">Nationality:</label>
<input type="text" name="nationality" id="nationality"><br>
<label for="bio">Bio:</label>
<input type="textarea" name="bio" id="bio"><br><br>-->
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
{% endblock %}
......@@ -3,27 +3,20 @@
{% block title %}Add New Book{% endblock %}
{% block content %}
<form action="/" method="post">
{{ 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.as_p }}
<!--<label for="title">Title:</label>
<input type="text" name="title" id="title"><br>
<label for="author">Author:</label>
<select id = "selectField" name="author">
{% for author in object.authors.all %}
<option value = {{ author }}>{{ author }}</option>
{% endfor %}
<input type="text" name="author" id="author"><br>
<label for="publisher">Publisher:</label>
<input type="text" name="publisher" id="publisher"><br>
<label for="publisher">Year Published:</label>
<input type="number" name="year_published" id="year_published"><br>
<label for="ISBN">ISBN:</label>
<input type="number" name="ISBN" id="ISBN"><br>
<label for="blurb">Blurb:</label>
<input type="textarea" name="blurb" id="blurb"><br><br>-->
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}Edit Author{% endblock %}
{% block content %}
{{ 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.as_p }}
<input type="submit" value="Edit Author">
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %}Edit Book{% endblock %}
{% block content %}
{{ 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.as_p }}
<input type="submit" value="Edit Book">
</form>
{% endblock %}
from django.urls import path
from .views import home_view, BooksDetailView, BooksView, AuthorsView, AuthorDetailsView, BookCreateView, AuthorCreateView
from .views import home_view, BooksDetailView, BooksView, AuthorsView, AuthorDetailsView, BookCreateView, AuthorCreateView, BookUpdateView, AuthorUpdateView
urlpatterns = [
path('home/', home_view, name='index'),
......@@ -10,6 +10,8 @@ urlpatterns = [
path('authors/<int:pk>/details/', AuthorDetailsView.as_view(), name='author-detail'),
path('books/add/', BookCreateView.as_view(), name='add-book'),
path('authors/add/', AuthorCreateView.as_view(), name='add-author'),
path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='edit-book'),
path('authors/<int:pk>/edit/', AuthorUpdateView.as_view(), name='edit-author'),
]
app_name = 'bookshelf'
\ No newline at end of file
......@@ -3,7 +3,9 @@ from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from django.http.response import HttpResponseRedirect
from .models import Books, Author
from .forms import BookForm, AuthorForm
def home_view(request):
return render(request, 'home.html')
......@@ -29,9 +31,39 @@ class BookCreateView(CreateView):
fields = '__all__'
template_name = "bookshelf/add-book.html"
def post(self, request,*args,**kwargs):
form = BookForm(request.POST)
if form.is_valid():
new_book = form.save()
redirect_url = "../" + new_book.get_absolute_url() +"/details/"
return HttpResponseRedirect(redirect_url)
else:
return render(request, self.template_name, {'form': form})
class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
template_name = "bookshelf/add-author.html"
def post(self, request,*args,**kwargs):
form = AuthorForm(request.POST)
if form.is_valid():
new_author = form.save()
redirect_url = "../" + new_author.get_absolute_url() + "/details/"
return HttpResponseRedirect(redirect_url)
else:
return render(request, self.template_name, {'form': form})
class BookUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = "bookshelf/edit-book.html"
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = "bookshelf/edit-author.html"
\ No newline at end of file
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