# Book database for a library system

I am writing a book database, and ultimately a library system, in Django. When a book is created without a title set, the ISBN will be used to automatically fetch book data from WorldCat I have just created a form to add new books to the database, but it has no real error-handling. Also, as I am new to Django, I suspect I have put a lot of my code in the wrong place.

Improving error-handling is my main incentive, but any other improvements would be welcome.

The form, in forms.py:

class AddBookForm(ModelForm):

class Meta:
model = Book
fields = ['isbn']


The form view, in views.py:

# This defines the navbar

if request.method == 'POST':
# create a form instance and populate it with data from the request:
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# if a GET (or any other method) we'll create a blank form
else:

# Highlights navbar entry
pages[1].append(True)
context = {'pages': pages, 'form': form,}
else:


{% extends "library/base_site.html" %}
{% block title %}Add book{% endblock %}
{% block content %}
<span aria-hidden="true">&times;</span>
<span class="sr-only">Close</span>
</button>
</div>
{% endif %}
<form role="form" method="post" action=".">
{% csrf_token %}
<div class="form-group">
<label for="id_isbn">Isbn</label>
<input id="id_isbn" class="form-control" maxlength="13" name="isbn" type="text" placeholder="Enter ISBN or scan barcode">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endblock %}


The entire project is available on GitHub.

This is a bit strange, and more complicated than it needs to be:

if 'addedBook' in locals():
else:


You could initialize addedBook = None, and simplify the above, and without using locals():

addedBook = None
# validation, possibly setting: addedBook = form.save()


Needless to say, this "docstring" is useless:

"""docstring for AddBookForm"""


### Error handling

Your current error handling is merely checking if the form is valid or not. And the form class is very basic, so only the standard validations will be applied, according to the underlying model, for example, non-integer values for an integer field will raise a validation error.

At the minimum, it would be good to tell the user about validation errors, if any. In the current code, if the form was not valid, it simply prints the same form again without giving the user a clue about what went wrong, which is not very user-friendly.

This page in the docs touches on the basics of form validation and includes examples of showing form validation errors:

https://docs.djangoproject.com/en/1.7/topics/forms/

https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/

I recommend you re-read them and:

1. Add printing of form errors to the HTML output
2. Add custom validation methods in the form class, if it makes sense for your use case
• This is very helpful, thank you. Any ideas on error-handling? – felixphew Sep 25 '14 at 7:07
• I added a section for that. – janos Sep 25 '14 at 7:55