Commit f32f0d42 authored by Albert Gagalac's avatar Albert Gagalac

added form rendering for both add-author.html and add-book.html

parent 2aa70d74
...@@ -35,14 +35,14 @@ ALLOWED_HOSTS = [] ...@@ -35,14 +35,14 @@ ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
"bookshelf.apps.BookshelfConfig", # "bookshelf.apps.BookshelfConfig",
"django.contrib.admin", "django.contrib.admin",
"django.contrib.auth", "django.contrib.auth",
"django.contrib.contenttypes", "django.contrib.contenttypes",
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
#"bookshelf" "bookshelf"
] ]
MIDDLEWARE = [ MIDDLEWARE = [
...@@ -57,10 +57,12 @@ MIDDLEWARE = [ ...@@ -57,10 +57,12 @@ MIDDLEWARE = [
ROOT_URLCONF = "albertgagalac_reading.urls" ROOT_URLCONF = "albertgagalac_reading.urls"
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
TEMPLATES = [ TEMPLATES = [
{ {
"BACKEND": "django.template.backends.django.DjangoTemplates", "BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [os.path.join(BASE_DIR, 'templates')], "DIRS": [os.path.join(SETTINGS_PATH, 'templates')],
"APP_DIRS": True, "APP_DIRS": True,
"OPTIONS": { "OPTIONS": {
"context_processors": [ "context_processors": [
......
from django import forms from django import forms
from .models import Author from .models import Author, Book
class AuthorForm(forms.Form): # class AuthorForm(forms.Form):
first_name = forms.CharField(label="First Name", max_length=100) # first_name = forms.CharField(label="First Name", max_length=100)
last_name = forms.CharField(label="Last Name", max_length=100) # last_name = forms.CharField(label="Last Name", max_length=100)
age = forms.IntegerField(label="Age",min_value=1) # age = forms.IntegerField(label="Age",min_value=0)
nationality = forms.CharField(label="Nationality", max_length=100) # nationality = forms.CharField(label="Nationality", max_length=100)
bio = forms.CharField(label="Bio", max_length = 700) # bio = forms.CharField(label="Bio", max_length = 700)
class BookForm(forms.Form): # class BookForm(forms.Form):
title = forms.CharField(label="Title", max_length=100) # title = forms.CharField(label="Title", max_length=100)
author = forms.ModelChoiceField(queryset=Author.objects.all()) # author = forms.ModelChoiceField(queryset=Author.objects.all())
publisher = forms.CharField(label="Publisher", max_length=100) # publisher = forms.CharField(label="Publisher", max_length=100)
ISBN = forms.IntegerField(label="ISBN") # ISBN = forms.IntegerField(label="ISBN")
blurb = forms.CharField(label="Blurb", max_length = 700) # blurb = forms.CharField(label="Blurb", max_length = 700)
\ No newline at end of file
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'publisher', 'ISBN', 'blurb']
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio ']
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block title %} Add new 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="/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block title %} Add new 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 %}<br>
<form action="/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
{% comment %} {% block forms %}
{{ 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="/index_card" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %} {% endcomment %}
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block title %} Edit book {% endblock %}
{% block forms %}
{{ 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="/index_card" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
\ No newline at end of file
...@@ -2,12 +2,18 @@ from django.urls import path ...@@ -2,12 +2,18 @@ from django.urls import path
from .views import ( from .views import (
AuthorDetailView, BookDetailView, AuthorDetailView, BookDetailView,
home, BookListView, AuthorListView home, BookListView, AuthorListView,
BookCreateView, AuthorCreateView,
BookUpdateView, AuthorUpdateView
) )
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path("authors/", AuthorListView.as_view(), name="author-list"), path("authors/", AuthorListView.as_view(), name="author-list"),
path("books/", BookListView.as_view(), name="books-list"), path("books/", BookListView.as_view(), name="books-list"),
path("books/add", BookCreateView.as_view(), name="books-add"),
path("authors/add", AuthorCreateView.as_view(), name="author-add"),
path("books/<int:pk>/edit", BookUpdateView.as_view(), name="books-edit"),
path("authors/<int:pk>/edit", AuthorUpdateView.as_view(), name="author-edit"),
path("books/<int:pk>/details", BookDetailView.as_view(), name="books-detail"), path("books/<int:pk>/details", BookDetailView.as_view(), name="books-detail"),
path("authors/<int:pk>/details", AuthorDetailView.as_view(), name="author-detail"), path("authors/<int:pk>/details", AuthorDetailView.as_view(), name="author-detail"),
] ]
......
from django.shortcuts import render from django.shortcuts import render
from django.views import View
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 django.views.generic.edit import CreateView, UpdateView
from .models import Author, Book from .models import Author, Book
...@@ -10,19 +11,45 @@ from .models import Author, Book ...@@ -10,19 +11,45 @@ from .models import Author, Book
def home(request): def home(request):
return render(request, 'bookshelf/home.html') return render(request, 'bookshelf/home.html')
class BookListView(ListView):
model = Book
class BookUpdateView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
class BookDetailView(DetailView): class BookDetailView(DetailView):
model = Book model = Book
template_name = 'bookshelf/book_detail.html'
class BookCreateView(CreateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/add-book.html'
# template to be used by default is called <model>_form.html
class AuthorListView(ListView):
model = Author
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
class AuthorDetailView(DetailView): class AuthorDetailView(DetailView):
model = Author model = Author
template_name = 'bookshelf/author_detail.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['book_list'] = Book.objects.filter(author=self.object) context['book_list'] = Book.objects.filter(author=self.object)
return context return context
class BookListView(ListView):
model = Book
class AuthorListView(ListView): class AuthorCreateView(CreateView):
model = Author model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
# template to be used by default is called <model>_form.html
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