How to set up a new Django project

This post is a continuation of the django multi-tenancy series. In this part, we'll begin to implement the backend of the multitenant Django app. We'll be setting up a Django project from scratch.

The project source code up until this point is available on the github setup branch.

The project's final outcome can be explored using this live demo.

defynetech

Installing project dependencies

For this project, we'll need to have python 3+ installed. You can find the necessary steps in this digital ocean post. Next, we'll need to make virtual environments for installing project dependencies.

Install virtualenv

pip install virtualenv

Create a new virtualenv

virtualenv yourenvname -p python3.8

Activate virtual environment

source yourenvname/bin/activate

Run this command within the directory the virtual environment was created in.

Once your virtual environment is activated, there's a number of dependencies we'll be needing for the project:

  • Django and the djangorestframework: for an extensive introduction on how to use these libraries, check out my previous post on building web apis with django.
  • Djoser: REST implementation of Django authentication system. The Djoser library provides a set of Django Rest Framework views and endpoints to handle basic actions such as registration, login, logout, password reset, and account activation
  • django-rest-framework-simplejwt: provides a JSON Web Token authentication backend for the Django REST Framework.

My previous post on setting up Django APIs with JWT authentication should get you started on what JWTs are and how to implement them in Django. 😃

  • python-decouple: Decouple helps you to organize your settings so that you can change parameters without having to redeploy your app. These settings can be stored in parameters in ini or .env files. The library also allows the definition of comprehensive default values. Case in point;
from decouple import config

SECRET_KEY = config('SECRET_KEY')
DEBUG=config('DEBUG', cast=bool, default=False)

The repository has a requirements.txt and you can mass install all the dependencies by running pip install -r requirements.txt

Curious on some of the popular dependencies I use with Django? This post has some handy tools and resources for python programmers.

To kick off the new Django project, run the following once you've activated the previously created virtual environment.

django-admin startproject budgeting

Now to start with the first app that we'll require for the next section:

cd budgeting python manage.py startapp users

users will be our first app for this project and we'll put all the authentication logic here.

Next up, we'll cover user authentication in a multi-tenant app powered by django.🤠

open to collaboration

I recently made a collaborations page on my website. Have an interesting project in mind or want to fill a part-time role? You can now book a session with me directly from my site.

Sponsors

Please note that some of the links below are affiliate links and at no additional cost to you. Know that I only recommend products, tools and learning services I've personally used and believe are genuinely helpful. Most of all, I would never advocate for buying something you can't afford or that you aren't ready to implement.

Scraper API

Scraper API is a startup specializing in strategies that'll ease the worry of your IP address from being blocked while web scraping.They utilize IP rotation so you can avoid detection. Boasting over 20 million IP addresses and unlimited bandwidth.

In addition to this, they provide CAPTCHA handling for you as well as enabling a headless browser so that you'll appear to be a real user and not get detected as a web scraper. Usage is not limited to scrapy but works with requests, BeautifulSoup and selenium in the python ecosystem. Integration with other popular platforms such as node.js, bash, PHP and ruby is also supported. All you have to do is concatenate your target URL with their API endpoint on the HTTP get request then proceed as you normally would on any web scraper. Don't know how to webscrape? Don't worry, I've covered that topic extensively on the webscraping series. All entirely free!

scraperapi

Using this scraper api link and the promo code lewis10, you'll get a 10% discount on your first purchase!! You can always start on their generous free plan and upgrade when the need arises.

Digital Ocean

Looking for cloud hosting for your projects? Digital Ocean is just the right partner for you.

They make it simple to launch in the cloud and scale up as you grow – with predictable pricing, team accounts, and more. Their intuitive control panel and API give you time to build more and spend less time managing your infrastructure.

Using this digitalocean link to sign up, you'll get $100 in credit for the first two months!

Share to social media