Skip to main content

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

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

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/dev/topics/forms/https://docs.djangoproject.com/en/1.7/topics/forms/

This page is more detailed, and specific about Model forms:

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/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 a bit strange, and more complicated than it needs to be:

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

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/dev/topics/forms/

This page is more detailed, and specific about Model forms:

https://docs.djangoproject.com/en/dev/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 a bit strange, and more complicated than it needs to be:

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

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/

This page is more detailed, and specific about Model 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
added 1048 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

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

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

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/dev/topics/forms/

This page is more detailed, and specific about Model forms:

https://docs.djangoproject.com/en/dev/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 a bit strange, and more complicated than it needs to be:

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

Needless to say, this "docstring" is useless:

"""docstring for AddBookForm"""

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

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

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/dev/topics/forms/

This page is more detailed, and specific about Model forms:

https://docs.djangoproject.com/en/dev/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
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

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

if 'addedBook' in locals():
    context['added'] = addedBook
else:
    context['added'] = None

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

addedBook = None
# validation, possibly setting: addedBook = form.save()
context['added'] = addedBook

Needless to say, this "docstring" is useless:

"""docstring for AddBookForm"""