Introduction

In Pixly, we are using Django as our backend due to the excellent data science libraries and the great community in the Python ecosystem. However, when we have decided to make the frontend app as a single-page-application with React, we faced many difficulties. We hope that our tutorial will help newcomers a bit for solving their future problems.

Requirements

People who follow this tutorial should have at least an elementary level of knowledge about Python and a basic level of knowledge Django framework. Also, the basic level of Javascript and React is a must.

This tutorial series is planned to be made of three parts:

  1. Django backend.
  2. Django GraphQL API.
  3. React frontend and bundling the client app with Webpack.

The Project Description

We will make a stupidly simple single page application written with React that will communicate with our Django backend via a GraphQL API. The React app will be written from scratch with webpack. The create-react-app boilerplate will not be used.

There are a few options to make Django and React integrated in production:

  1. You may run two servers, one is Django backend and one for Node backend server. This is not ideal because of the costs associated with running two different servers.
  2. You may use serverless architecture, so you only pay for the functions that are triggered by client-side and query your database with it.
  3. You can run a Django backend server that will respond to our frontend application’s requests. This will be our approach in this series.

You may also like:
Python Django Tutorial for Beginners.

Our frontend application will run on our users’ browsers. All the frontend requests and logic will be done by the browser with a React JavaScript application.

Our project will not be an isomorphic application. Therefore, we need an API that will be used for the communication of Python backend and JavaScript frontend. We have two options for that; REST and GraphQL. We will use GraphQL. Our frontend and backend will use Apollo GraphQL and the Graphene framework respectively.

Maybe it is not the best solution, but we will use two servers in development. In production, we will use only one. Our bundled frontend app will not need a server.

Step 0: Configuring Environment

Note: If you choose to use your existing Python environment, you can skip this part and go to step 1.

This tutorial was written on a computer with Ubuntu 18.04 operating system. The default Python interpreter is Python 3.6.x. Due to the need for a clean environment, Python 3.8 will be installed, and the virtual environment will be built on it.

Create a Django Project

Step 1: Install Dependencies

Step 2: Create a Django Project

Step 3: Create a New Django App

Now, we can create our app in order to create database models.

Step 4: Configure Django Settings

Update your setting file according to this.

Now, the general structure of the directory should look like this:

Directory structure

Directory structure

Step 5: Run the Django Server

Before starting our project, we should first make a database migration.

Now, we can start our project and see what happens.

Development server running

Development server running

Now, Our Django development server is ready.

You can go to the URL address of http://127.0.0.1:8000/ and verify that it is working.

Django default display

Django default display

After then, you can stop the server by pressing CTRL + C.

Step 6: Create the Movie App

We will create a movie model with basic fields that a movie should have. Before that, we should give some information about the field choices.

Why is there a URL field for poster rather than image field?

Because serving static files in production is out of our scope, we decide to use only the URL field. Fetching the image from remote and then saving it to our production storage is a topic for another post. Due to this, we will save only the poster’s URL, not the poster itself as an image file. Also, sending static files like images is not a good approach. We will send the exact URL of an image to the user. Then, the user’s browser fetches the image from the URL.

What is slug and why should it be unique?

When browsing on Pixly, if you open  The Matrix movie page, you will see that your address bar will be: https://pixly.app/movie/the-matrix-1999. The last part of the URL is the slug of The Matrix movie, which acts as an identifier which makes the URL distinctive from other movie pages. In the GraphQL part of the tutorial, you will see that this slug will be used as a query parameter, meaning that database queries will be done according to slug. Therefore, it should be unique.

We can also choose the movie id as a URL identifier, but it’s clear that the URL will not be a human-readable address. Moreover, search engine indexing and ranking is a vital part of any website targeting new users. Readable URL addresses are good for users themselves and also suggested by search engine guides. For example, the Google webmaster guideline recommends using clean and concise URL structures. Let’s make our model.

Let’s open the items/models.py file.

Migrate again in order to make database arrangements for our new model.

Step 7: Populating Database With Initial Data

There is no movie record currently in our database. We will provide a small initial data to create some movie records. All the data is provided by the community-built The Movie Database (TMDb).

First, create a class method in order to import initial data.

Then, get the initial data file from GitHub repo and set the initial_data.py file in the utils folder. The directories and the initial data looks like this.

Initial data

Normally, we would have an open Django shell. However, shell_plus, which is provided by the django_extensions package, is more functional, so we will use this.

Our model and database are ready. You can close the shell with the quit command.

In the next part, we will create our API with Python’s GraphQL framework, Graphene, and its Django package graphene-django.

Further Reading



Source link

Write A Comment