Django – CRUD application with Python

Making a CRUD application with Python is actually one of the basic thing a Python Developer should be able to do. Although some may argue it is extremely basic – it is not.

Requirements

The basic requirements for this one, were the following:

  • Display “There are no recipes”, if there are no recipes written:

Index.html looks like this, when there are no recipes present.

  • Have a nice form to create a recipe:

    Creating a recipe is powered by the Forms in Django.

  • Delete the recipe with a blurred option:

    The form is not clicable, see the DisabledForm() class.

  • Display the recipes nicely, one below another:

    The index.html displays the recipes, if there are any.

  • Have a database (this is not Excel and VBA where you can save things in a spreadsheet 🙂 )
  • Show “details” nicely, with ingredients splitted by comma and shown to a list:

    The details.html page. The ingredients are splitted and listed.

Code

Ok, the code is actually available in GitHub (scroll a bit more), but here are some of the highlights:

Forms

The forms are part of the “Django” magic – they take the class which they model and render it into HTML page

The DisabledForm() class is the one that makes the form look “unclickable”. See the “Delete Recipe” picture above to get an idea:

Views

The views are the part of the code, that take care about which html file should be presented with what content, upon a given request. In the project, these are into 2 files. Index.py is actually rather straight-forward, presenting either a simple html file with “There are no recipes” text on it or listing the recipes above each other. Which version you will see, depends on the value of the recipes_present key of the context:

For the other recipes.py views the story is not that trivial. There we see a difference between GET and POST requests, for each of the CRUD actions. A pk variable is taken, in oder to locate exactly the correct recipe from the database with recipe = Recipe.objects.get(pk=pk):

Model

Our model is only one, as the app is quite small. Anyway, this one is used for the forms creation:

Urls

There are two type of urls – /app/views/urls.py  and /recipes/urls.py. The old Roman saying – “Divide and rule” is actually quite an important one here, putting the recipe app logic into the recipe app and leaving almost nothing to the main app:

Templates

Partial template

As you have probably noticed that the upper part of the app is repeating everywhere, it is a good idea to take this part of the app into a partial template. It looks like this:

Index.html

I have decided to put the Index, as you may easily see the If-Else-EndIf logic in this one, deciding whether to render the “No recipe” part of the page or the pictures with the text.

Delete.html

The delete is actually chosen, because it provides just a small taste of how little code is needed, if one uses the partial site and the forms correctly. 15 lines are enough:

How to run it & GitHub:

I guess you probably will not be able to replicate the project with the code above. Don’t worry, there is a zip archive in git, that you can actually extract and run: https://github.com/Vitosh/Python_personal/tree/master/PythonProjects/recipes_su

Still, just having the GitHub will not be enough. There is a database behind it and if you run it without setting the database,  you would get: conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL: database “recipes” does not exist

  • First make sure that you have a database named “recipes”;
  • Then make sure that you run migrate  from manage.py;
  • The settings.py is another story;
Tagged with: , , , , , , ,