Commit c8d11529 authored by Joaquin Hermosisima's avatar Joaquin Hermosisima

created forms and new templates

parent 0c64469b
from django import forms
from .models import Author, Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = (
'title',
'author',
'publisher',
'year_published',
'ISBN',
'blurb',
)
labels = {
'title': 'Title',
'author': 'Author',
'publisher': 'Publisher',
'year_published': 'Year Published',
'ISBN': 'ISBN',
'blurb': 'Description',
}
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control'}),
'author': forms.Select(attrs={'class': 'form-control'}),
'publisher': forms.TextInput(attrs={'class': 'form-control'}),
'year_published': forms.NumberInput(attrs={'class': 'form-control'}),
'ISBN': forms.TextInput(attrs={'class': 'form-control'}),
'blurb': forms.Textarea(attrs={'class': 'form-control'}),
}
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = (
'first_name',
'last_name',
'age',
'nationality',
'bio'
)
labels = {
'first_name': 'First Name',
'last_name': 'Last Name',
'age': 'Age',
'nationality': 'Nationality',
'bio': 'Bio',
}
widgets = {
'first_name': forms.TextInput(attrs={'class': 'form-control'}),
'last_name': forms.TextInput(attrs={'class': 'form-control'}),
'age': forms.NumberInput(attrs={'class': 'form-control'}),
'nationality': forms.TextInput(attrs={'class': 'form-control'}),
'bio': forms.Textarea(attrs={'class': 'form-control'}),
}
{% extends "base.html" %}
{% block content %}
<h1>Add New Author</h1>
<form action="{% url 'bookshelf:authorcreate' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Add Author</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% block title %}Add New Book{% endblock %}
{% block content %}
<h1>Add New Book</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Add Book</button>
</form>
{% if form.errors %}
<div class="alert alert-danger" role="alert">
<strong>Oops! Something went wrong.</strong> Please check the form and try again.
</div>
{% endif %}
{% endblock %}
{% extends "base.html" %} {% extends "base.html" %}
{%block title%}{{object.first_name}} {{object.last_name}}{%endblock%} {%block title%}{{object.first_name}} {{object.last_name}}{%endblock%}
{% block content %} {% block content %}
<h1>{{object.first_name}} {{object.last_name}}</h1>
<h2>{{object.first_name}} {{object.last_name}}</h2>
<ul> <ul>
<li>{{object.age}}</li> <li>{{object.age}}</li>
<li>{{object.nationality}}</li> <li>{{object.nationality}}</li>
<li>{{object.bio}}</li> <li>{{object.bio}}</li>
</ul> </ul>
<h3>Books by {{object.first_name}} {{object.last_name}} I love:</h3> <a href="{% url 'bookshelf:authoredit' object.pk %}" class="btn btn-primary"><button>Edit Author</button></a>
<br>
<h2>Books by {{object.first_name}} {{object.last_name}} I love:</h2>
<br>
<ul> <ul>
{% for item in book_list %} {% for item in book_list %}
<li><a href="../../book/{{item.pk}}/details"">{{item.title}}</a></li> <li><a href="../../book/{{item.pk}}/details"">{{item.title}}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
<br><br><br> <br><br><br>
{% for url in url_list %} {% for url in url_list %}
<a href="../../{{url}}">{{url}}</a> <a href="../../{{url}}">{{url}}</a>
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}{{object.title}}{% endblock %} {% block title %}{{object.title}}{% endblock %}
{% block content %} {% block content %}
<h1>{{object.title}}</h1>
<h2>{{object.title}}</h2>
<ul> <ul>
<a href="{{object.author.get_absolute_url}}"><li>{{object.author}}</li></a> <a href="{{object.author.get_absolute_url}}"><li>{{object.author}}</li></a>
<li>{{object.publisher}}</li> <li>{{object.publisher}}</li>
...@@ -13,6 +11,7 @@ ...@@ -13,6 +11,7 @@
<li>{{object.blurb}}</li> <li>{{object.blurb}}</li>
</ul> </ul>
<a href="{% url 'bookshelf:bookedit' object.pk %}" class="btn btn-primary"><button>Edit Book</button></a>
<br><br><br> <br><br><br>
{% for url in url_list %} {% for url in url_list %}
......
{% 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 action="{% url 'bookshelf:authoredit' pk=form.instance.pk %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</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 action="{% url 'bookshelf:bookcreate' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
from django.urls import path from django.urls import path
from .views import BookListView, BookDetailView, AuthorListView, AuthorDetailView from .views import (BookListView, BookDetailView, AuthorListView, AuthorDetailView,
BookCreateView, BookUpdateView, AuthorUpdateView, AuthorCreateView)
urlpatterns = [ urlpatterns = [
path('books/', BookListView.as_view(), name='book-list'), path('books/', BookListView.as_view(), name='book-list'),
path('book/<int:pk>/details', BookDetailView.as_view(), name='bookitem'), path('book/<int:pk>/details', BookDetailView.as_view(), name='bookitem'),
path('authors/', AuthorListView.as_view(), name= 'author-list'), path('authors/', AuthorListView.as_view(), name= 'author-list'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(), name="authoritem") path('authors/<int:pk>/details', AuthorDetailView.as_view(), name="authoritem"),
path('books/add/', BookCreateView.as_view(), name="bookcreate"),
path('authors/add/', AuthorCreateView.as_view(), name="authorcreate"),
path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='bookedit'),
path('authors/<int:pk>/edit/', AuthorUpdateView.as_view(), name='authoredit'),
] ]
app_name='bookshelf' app_name='bookshelf'
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from .models import Author, Book from .models import Author, Book
from .forms import BookForm, AuthorForm
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse, reverse_lazy
class BookListView(ListView): class BookListView(ListView):
model = Book model = Book
...@@ -40,3 +43,43 @@ class AuthorDetailView(DetailView): ...@@ -40,3 +43,43 @@ class AuthorDetailView(DetailView):
context['book_list'] = Book.objects.filter(author=self.object.pk) context['book_list'] = Book.objects.filter(author=self.object.pk)
return context return context
def add_book_view(request):
form = BookForm()
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
form.save()
return redirect('bookshelf:bookitem', pk=form.instance.pk)
return render(request, 'bookshelf/add-book.html', {'form': form})
class BookCreateView(CreateView):
model = Book
form_class = BookForm
template_name = "bookshelf/add-book.html"
def get_success_url(self):
return reverse('bookshelf:bookitem', args=(self.object.pk,))
class BookUpdateView(UpdateView):
model = Book
form_class = BookForm
template_name = "bookshelf/edit-book.html"
def get_success_url(self):
return reverse_lazy('bookshelf:bookitem', args=[self.object.pk])
class AuthorUpdateView(UpdateView):
model = Author
form_class = AuthorForm
template_name = "bookshelf/edit-author.html"
def get_success_url(self):
return reverse_lazy('bookshelf:authoritem', args=[self.object.pk])
class AuthorCreateView(CreateView):
model = Author
form_class = AuthorForm
template_name = "bookshelf/add-author.html"
def get_success_url(self):
return reverse('bookshelf:authoritem', args=(self.object.pk,))
\ 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