Building a RESTFul RecipeAPI with Django and SQlite

Alexander Nyaga - Sep 1 - - Dev Community

Introduction:

In web development, creating robust and scalable apps is the most important goal. Django, a high-level Python web framework, provides a comprehensive toolkit for easily developing sophisticated web applications. When creating RESTful APIs, the Django Rest Framework (DRF) stands out as a top solution, offering a comprehensive collection of tools to tackle the complexities of API development.

This article will walk you through creating a simple yet working Recipe CRUD (Create, Read, Update, Delete) application with Django and the Django Rest Framework. We will explore the core concepts, step-by-step implementation, and best practices for ensuring your application is both stable and manageable. Whether you are a seasoned developer or just starting with Django and DRF, this article will provide a simple yet comprehensive path to creating a fully usable CRUD API.

Prerequisites:

Before getting started, make sure you have the following installed:

Python: You can download Python from the official website (https://www.python.org/downloads/) and install it based on your operating system.
Django and the Django REST framework: Install Django and the Django REST framework using pip (Python package manager) by running the following command:

Step 1: Set up your project:

We start by setting up a new Django project and app:

In your terminal, create a new directory for the project:

  1. Create a virtual environment and activate it:

Image description

  1. Create a new Django project and app:

Step 2: Register the app and REST framework in settings:

Step 3: Define the Food model

Step 4: Database Configurations:
We are using the default database that comes with django:

Step 5: Create the API views:

Step 6: Create the API serializer

Step 7: Configure the URLs:

Step 8: Run the development server:

Testing the API:

When the server starts, you should see an output showing that the development server is up and running. By default, it listens at http://127.0.0.1:8000/ or http://localhost:8000/.

To interact with and read responses from your API endpoints, use a tool like curl, Postman, or a web browser.

  1. To test the recipe list, navigate to http://localhost:8000/recipe_app/ in your web browser or use curl to send a GET request to the URL.

  2. To test the recipe_detail view, enter the ID of the recipe item you want to see. For example, go to http://localhost:8000/recipe_app/1/ in your web browser or use curl to send a GET request to the URL.

  3. To test the create_recipe view, use curl to make a POST request to http://localhost:8000/recipe_app/ with the data for creating a new recipe.

After creating a new recipe, you receive a message,a new recipe was successfully created.

  1. To test the update_recipe view, use Postman to make a PUT request to http://localhost:8000/recipe_app/ with the data for updating an existing recipe. Replace with the ID of the recipe you want to update. Before updating

  2. To test the delete_recipe view, use postman to make a DELETE request to http://localhost:8000/recipe_app/ /. Replace with the ID of the food item you want to delete:

Check the server’s responses to ensure that the API is working properly.

Remember to tailor the testing to your individual API requirements and use case. You can also utilize tools like Postman to provide a more user-friendly API testing experience.

Conclusion:

With Django and SQlite, you have successfully constructed a Recioe API. The API offers endpoints for creating new recipes, updating current recipe, deleting recipes, and listing all recipes. You can modify this API further in accordance with the needs of your project or use it as a basis for developing more intricate django projects.

When putting your API into production, don’t forget to take the necessary security precautions, such as managing authorization and authentication as needed.

Github link:https://github.com/alexander784/Recipe_API

. .
Terabox Video Player