Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding URLs and Using the API

00:00 Adding URLs and Using the API. To add URLs Django REST Framework provides a DefaultRouter that will automatically generate URLs for a ModelViewSet.

00:12 This will save time and reduce the possibility of errors. Create a urls.py file in the countries application folder and add the code seen on screen.

00:46 This creates a DefaultRouter() and registers CountryViewSet under the country’s URL. This will place all the URLs for CountryViewSet under countries.

01:04 Note that Django REST Framework automatically appends a forward slash to the end of any endpoints generated by DefaultRouter. You can use the option seen on screen to disable this.

01:18 This will disable the forward slash at the end of the endpoints and make them consistent with the other two APIs you’ll create in this course. Finally, you’ll need to update the project’s base urls.py file to include all the countries URLs in the project.

01:34 Update the urls.py file inside of the country_api/ folder with the code seen on screen.

01:51 Now you are ready to try out your Django-backed REST API. Run the command seen on screen in the root country_api/ directory to start the Django development server.

02:01 The development server is now running. Go ahead and send a GET request to countries. Note there’s no trailing slash to get a list of all the countries in your Django project.

02:21 Django REST Framework sends back a JSON response with the three countries you added earlier. The response you get may need formatting for readability, so on screen you can see it made human readable.

02:35 The DefaultRouter you created in countries/urls.py provides the URLs seen on screen for all the standard endpoints. You can try out a few more endpoints next. Send a POST request to countries to create a new country in the Django project.

03:10 This creates a new country with the JSON you sent in the request. Django REST Framework returns a 201 Created status code and the new country.

03:20 By default, the response doesn’t include a new line at the end. This means the JSON may run into your command prompt. The curl command you just used includes the -w option to add a newline character after the JSON to fix this issue.

03:35 You can view an existing country by sending a request to GET countries/<country_id> with an existing ID. Run the command seen on screen to get the first country.

03:52 The response contains the information for the first country. These examples only covered GET and POST requests, but feel free to try out PUT, PATCH, and DELETE requests on your own to see how you can fully manage your model from the REST API. As you’ve seen, Django REST Framework is a great option for building REST APIs, particularly if you have an existing Django project and you want to add API functionality to it.

04:17 But if you’re starting out from scratch, there’s another popular option, which as its name suggests, is aimed at creating APIs: FastAPI. And that’s what you’ll be looking at in the next section of the course.

Become a Member to join the conversation.