In this lesson, we are going to explore how to create a basic API endpoint using Django and Django REST Framework (DRF). An API endpoint is a URL where your client applications can interact with your server-side resources. This is a crucial aspect of web development because it allows your application to communicate over the network.
The Django REST Framework makes it easier to build and manage APIs with Django. Let's get started by understanding the role of an API endpoint and why Django REST Framework is a powerful tool for this purpose.
Before we dive into the new content, let's quickly recap the initial setup we covered in the previous lesson:
Django and Django REST Framework.Django project and a new app.DRF.Now that we are all set up let's proceed to create a basic API endpoint.
When working with APIs, you'll frequently come across different HTTP methods like GET, POST, PUT, DELETE, etc. These methods specify the type of operation to perform on the server-side resource.
For this lesson, we will define a simple GET request that returns a friendly greeting message.
An API endpoint is a specific URL through which clients can interact with server-side resources. A view in Django handles the logic for processing requests and returning responses for a specific endpoint.
To create our first API endpoint, we will use the APIView class provided by Django REST Framework. An APIView allows us to define different methods (like get, post, etc.) to handle different HTTP requests.
Here's how you can set up a HelloWorld APIView:
APIView to create our view and Response to send back an appropriate response.HelloWorld class: We create a class HelloWorld that inherits from APIView.get method: This method handles GET requests. Inside the method, we return a JSON response with a simple message.When a GET request is sent to this view, it will return a response saying {"message": "Hello, world!"}.
Next, we must make our APIView accessible via a URL. This involves mapping our HelloWorld view to a URL pattern.
Here is how you can do it:
path to define our URL patterns and HelloWorld from our views./hello/ URL to the HelloWorld view.By navigating to http://localhost:8000/hello/, the HelloWorld view will be triggered, and you will receive the {"message": "Hello, world!"} response.
Note that we define a name for our path with name='hello_world'. The name parameter in URL patterns allows you to:
reverse() function. This function takes the name of a URL pattern and returns its corresponding URL string. For example, if you have a named URL pattern hello_world, you can use reverse('hello_world') anywhere in your Django project to get the URL '/hello/'.Now it's time to test our newly created API endpoint. In actual development, you can do it in various ways, including manual testing with a web browser, some tools like postman or curl, and automated Django tests.
We will use the requests library, a popular HTTP library for Python, to send a request to our server and print its output.
Here is a simple Python script to test our API endpoint:
The requests.get(URL) function sends a GET request to the specified URL, and response.text prints the server's response.
If you create a Django project on your local machine, the URL will usually be http://127.0.0.1:8000/. However, in the CodeSignal environment, we will run the server on http://0.0.0.0:3000/. Important note: if you will shut down and restart the server manually with python3 manage.py runserver, it will be run at http://127.0.0.1:8000/, as it is a default Django setting. When solving tasks, pay attention to the address your server is running at and modify the send_request.py script accordingly.
Important note: If you see ConnectionRefusedError: [Errno 111] Connection refused in the terminal, there are two main reasons for it to happen:
In this lesson, we covered the process of creating a basic API endpoint using Django and Django REST Framework. We:
HelloWorld APIView.Creating a basic API endpoint is a fundamental skill in web development. This lesson marks a significant step in building our TODO app API.
Up next, you will get to practice what you've learned through a series of exercises. These practice exercises will reinforce your understanding and help you become more comfortable with the concepts covered in this lesson. Keep going, and you'll soon be building more complex API endpoints!

send_request.py file sends requests to an address other than the one where the server runs. Go to the terminal and examine the address.