HelloGitHub- dream chasers

In the first step of the HelloDjango Full Stack tutorial series, Django Blog Tutorial (second edition), we developed a fully functional personal blog system step by step.

Next, we will use Django-REst-Framework to develop RESTful apis for blogging systems step by step. In the process, we will learn how to use Django-Rest-Framework. Support backend apis for developing blog applications using Vue (the Vue tutorial will be available after the Django REST Framework tutorial is completed).

If you’ve already followed the first tutorial, Django Blog Tutorial (version 2), you can continue with your original project, with only minor differences from this tutorial.

You can also fork the new repository project, which is almost identical to the sample repository code in djangos blog tutorial (version 2), with some changes (such as Docker container names, image names, etc.) to reflect the features of this tutorial.

You can follow this tutorial smoothly based on any warehouse.

After the project code is ready, first to set up the local development environment, the process is very simple, only need to execute a few commands can be. Virtualenv or Docker can be started by referring to the README of the project.

Note:

Virtualenv or Pipenv is used to launch your project without setting up Elasticsearch. Please set the environment variable ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no to turn off the real-time index. Otherwise, blog posts cannot be created. If you turn off the real-time index, the full-text search function is unavailable.

Set ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no

Linux or macOS: export ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no

If you use Docker, you don’t need to set it because a Docker container containing Elasticsearch will automatically start.

The first step is to go to the root directory of the project and install the dependencies needed to start the project. For virtual environment management, if you are not familiar with it, please refer to:

# -- The dev parameter instructs PipenV to install both run-time and development-time dependencies

$ pipenv install --dev

Copy the code

Second, generate the database file.

$ pipenv run python manage.py migrate

Copy the code

Step 3: Create a background administrator account.

$ pipenv run python manage.py createsuperuser

Copy the code

For details, please refer to creation Background Open, please start your performance.

Step 4, run the development server.

$ pipenv run python manage.py runserver

Copy the code

Browser to access http://127.0.0.1:8000/ can enter the blog’s front page, http://127.0.0.1:8000/admin for blog background, can be used The third step to create the super administrator account login.

Step 5, run a script to generate some virtual test data for development and testing.

$ pipenv run python -m scripts.fake

Copy the code

In this way, the basic project has been successfully launched

Now you can start developing RESTful apis. Since you’re going to use Django-rest-Framework, the first thing you should do is install it. Go to the project root directory and run:

$ pipenv install djangorestframework django-filter

Copy the code

Django-filter is a library that provides API query result filtering, which we’ll talk about later.

Add Django-rest-framework to INSTALLED_APPS:

blogproject/settings/common.py



INSTALLED_APPS = [

.

    'rest_framework'.

]

Copy the code

Django-rest-framework also provides a background for interacting with an API. Sometimes the API may require login authentication, so add the django-Rest-Framework API interactive background and login authentication URL:

blogproject/urls.py



from rest_framework import routers



router = routers.DefaultRouter()

urlpatterns = [

.

    path("api/", include(router.urls)),

    path("api/auth/", include("rest_framework.urls", namespace="rest_framework")),

]

Copy the code

Here we use the Django-rest-framework Router, which automatically registers view functions to the specified URL path. DefaultRouter classes will help us to generate a default API interaction of root view of the background, direct access to the http://127.0.0.1:8000/api/ to get into API interactive background.


This page lists all available apis, and since no apis are being developed, the list is empty.

Click Log in in the upper right corner to authenticate login. After login, you can interact with the API that requires authenticated login.

This API interaction background is a powerful tool for our development and debugging, and we will continue to touch it in the future, when you can feel its role.

Now that django-Rest-Framework installation and basic setup is complete, it’s time to start developing the blog API.