How to Clone and Run a Django Project Locally

Open-source projects are increasingly in demand, and there’s a huge number to sink your teeth into. To start working on an open-source project, it helps if you can easily clone the source code and set the project up locally.

Although cloning may seem easy, it can be challenging with projects built with Django. Django has many dependencies and packages that cause conflicts when not installed.

4

You must fix the conflicts before the project can run on your local machine. Well, you don’t have to suffer anymore.

In the next steps, you will learn how to clone, set up, and run your Django project with minimal conflicts.

GitHub repositories on a computer screen

What You Will Need

To get the most from this guide, you should have the following:

With these requirements fulfilled, you’re ready to clone a project.

Image shows the Django project to be cloned on GitHub

1. Clone the Project From GitHub

it’s possible to clone a sample project fromGitHuband configure it on your local machine.

To clone the project, click on the green button labeledCode.On the dropdown, select and copy either HTTP or SSH links. These links are the GitHub URLs for the project. Any of them will do.

Image shows newly created database in system

When you want tocontribute to an open-source project using Git, you will first have toforkthe project from the project’s repository to make a copy before cloning.

Back to your machine, set up a folder namedclone_boma. Remember to navigate into the folder with thecd clone_bomacommand.

Image shows project landing page

Next, run the following command to clone the project into the folder.

So, to clone the sample project over HTTP, use this command:

2. Inspect the Project Files

After cloning,use the ls commandto check the files from the Boma-watch project are present. Typelsto list all the content in the folder.

You can use a text editor of your choice to inspect the contents of each file or view them on the GitHub website.

3. Set Up a Virtual Environment

You need to set up a virtual environment for the project. The sample project comes with a Pipenv environment which specifies the project’s dependencies in two files:PipfileandPipfile.lock.

You will encounter this error if you run the project with the old Pipfile:

ModuleNotFoundError: No module named ‘distutils.core’

You need to delete the virtual environment and install it afresh. To delete the virtual environment and its dependencies, run the following commands consecutively.

To remove the old environment:

To remove both Pipfiles:

To install your Pipenv dependencies on your python version:

Next, run the following command to install all Pipfiles packages:

Then activate the virtual environment:

You will see the virtual environment activated on the right side of your terminal window.

If you are using Venv to create your virtual environment, run the following to install dependencies:

To list out all the installed dependencies:

4. Create a Database

You need to install a database to handle the project data. You can choose any kind of database that suits your needs. For the sample project, you should use PostgresSQL.

Learn how toinstall PostgreSQL on Ubuntuorinstall PostgreSQL on Windows.

Once all is set, start the Postgres shell using the following commands:

Then create a database namednew_bomaon the server:

The server returns the word CREATE DATABASE when it creates a database. Confirm that the database exists bylisting all databases in the system with the command\l:

Insettings.py, you need to connect the database to the application. Do this by replacing the database user, name, and password with your own local values:

Also, remember to changeTIME_ZONEin the settings.py file to suit your location. This will keep your app updated.

Ensure you have installedPsycopg2to accommodate any images available:

5. Generate a Secret Key

Every Django project has a uniquesecret key.Usually, the secret key is not exposed online. It should be in anenvfile which you should include ina .gitignore fileto exclude from the repository.

You have to generate a new one for your project to run. Create a new secret key with a secret key generator, likeDjecrety.

6. Migrate Project to the Database

Run the following command to create tables for the app in the new database:

Then run the following to migrate:

When you run thepython manage.py migratecommand, you experience errors. The errors are notifying you of missing modules. check that to install all modules before you migrate.

When you have all the dependencies installed, run the migration. If the migrations finish, it’s time to run the project.

When you have all the dependencies installed, run the migration. If the migrations run, it’s time to run the project.

7. Run the Project

Run the project to ensure everything is okay before adding your contribution. Run the application with the following command:

If there are no errors, openhttp://127.0.0.1:8000/in a web browser.You will see the landing page of the project as shown below:

The Secret to Cloning a Django Project

Cloning Django projects helps to optimize your time. You can concentrate on developing new features instead of starting a project from scratch.

The secret to cloning and running a Django project on a local machine is to know how to handle the dependencies. After cloning, ensure you create a new virtual environment, install dependencies, and connect to a reliable database system.

Django is a powerful Python library. Learn how to clone and use its projects to create amazing applications that impact the tech community.

Django is a fast, scalable framework for web development in Python. Learn how to get started by installing Django and setting up a sample project.

Sometimes the smallest cleaning habit makes the biggest mess.

you could’t call this offline, Notion.

These are the best free movies I found on Tubi, but there are heaps more for you to search through.

Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.

When your rival has to bail out your assistant.

Technology Explained

PC & Mobile