Added links in homepage and forms

Added the 'Add' hyperlinks for author and books as well as created the forms where the user will be redirected when hyperlinks are clicked.
parent 74cadf02
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__"
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
# Create your models here. # Create your models here.
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=255) first_name = models.CharField(max_length=255)
...@@ -28,4 +29,5 @@ class Books(models.Model): ...@@ -28,4 +29,5 @@ class Books(models.Model):
return '{}'.format(self.title) return '{}'.format(self.title)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk': self.pk}) return reverse('bookshelf:books-detail', kwargs={'pk': self.pk})
\ No newline at end of file
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Add New 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 %}
{% for field in form %}
{{ field.label }}:{{field}} <br><br>
{% endfor %}
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% 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 method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}:{{field}} <br><br>
{% endfor %}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
find out that they have problematic views as a person. </h3> find out that they have problematic views as a person. </h3>
<a href="/books">Books</a> <a href="/books">Books</a>
<a href="/authors">Authors</a> <a href="/authors">Authors</a><br>
<a href="/books/add">Add Book</a> <a href="/books/add">Add Book</a>
<a href="/authors/add">Add Author</a> <a href="/authors/add">Add Author</a>
{% endblock %} {% endblock %}
from django.urls import path from django.urls import path
from .views import home, BooksListView, BooksDetailView, AuthorListView, AuthorDetailView from .views import (
home, BooksListView, BooksDetailView, AuthorListView,
AuthorDetailView, BooksCreateView, AuthorsCreateView
)
urlpatterns = [ urlpatterns = [
...@@ -8,7 +11,11 @@ path('home/', home, name='home'), ...@@ -8,7 +11,11 @@ path('home/', home, name='home'),
path('books/', BooksListView.as_view(), name='books-list'), path('books/', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'), path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'),
path('authors/', AuthorListView.as_view(), name='authors-list'), path('authors/', AuthorListView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(), name='author-detail') path('authors/<int:pk>/details/', AuthorDetailView.as_view(), name='author-detail'),
path('books/add/', BooksCreateView.as_view(), name = 'add-book'),
path('authors/add/', AuthorsCreateView.as_view(), name = 'add-author')
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books from .models import Author, Books
...@@ -21,4 +22,15 @@ class AuthorListView(ListView): ...@@ -21,4 +22,15 @@ class AuthorListView(ListView):
class AuthorDetailView(DetailView): class AuthorDetailView(DetailView):
model = Author model = Author
template_name = "bookshelf/author_details.html" template_name = "bookshelf/author_details.html"
\ No newline at end of file
class BooksCreateView(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class AuthorsCreateView(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.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