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.
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.
What You Will Need
To get the most from this guide, you should have the following:
Python (Python3 and above) A working knowledge of the Python-Django Library Pip3 Familiarity with Python virtual environments Basic knowledge of Git and GitHub A GitHub account Git installed on your local machine Familiarity with the command line
With these requirements fulfilled, you’re ready to clone a project.
1. Clone the Project From GitHub
You can clone a sample project from GitHub and configure it on your local machine.
To clone the project, click on the green button labeled Code. 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.
Back to your machine, set up a folder named clone_boma. Remember to navigate into the folder with the cd clone_boma command.
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 command to check the files from the Boma-watch project are present. Type ls to 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: Pipfile and Pipfile.lock.
You need to delete the virtual environment and install it afresh. To delete the virtual environment and its dependencies, run the following commands consecutively.
ModuleNotFoundError: No module named ‘distutils.core’
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 to install PostgreSQL on Ubuntu or install PostgreSQL on Windows.
Once all is set, start the Postgres shell using the following commands:
Then create a database named new_boma on the server:
The server returns the word CREATE DATABASE when it creates a database. Confirm that the database exists by listing all databases in the system with the command \l:
In settings.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 change TIME_ZONE in the settings.py file to suit your location. This will keep your app updated.
Ensure you have installed Psycopg2 to accommodate any images available:
5. Generate a Secret Key
Every Django project has a unique secret key. Usually, the secret key is not exposed online. It should be in an env file which you should include in a .gitignore file to 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, like Djecrety.
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 have all the dependencies installed, run the migration. If the migrations run, it’s time to run the project.
When you have all the dependencies installed, run the migration. If the migrations finish, 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, open http://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.