Setting up a cloned Django project
One of the things I miss about my JavaScript days is the ease of cloning a repository and getting the dependencies installed. JavaScript projects tend to have a file called package.json which has all the dependencies listed. Simply running $ npm install from the command line was enough to get going.
Despite being two months into my Django journey, simple routines like getting a project set up are far from second nature.
1. Clone the repository
git clone https://github.com/<username>/<forked-repo>.git
2. Create your own virtual environment
python3 -m venv venvsource venv/bin/activate
Virtual environments are where dependencies are stored, similar to node_modules in JavaScript. Every time you start your machine, you must activate the virtual environment using source venv/bin/activate.
3. Install your requirements
pip install -r requirements.txt
4. Create a new PostgreSQL database
For this, I’m assuming you already have pgAdmin and postgres installed. Apologies for the lack of detail here.
In your terminal:
$ psql postgres$ CREATE DATABASE databasename$ \connect databasename
Go into pgAdmin, login, and check that the new database exists on the dbserver.
The database credentials to go in your project’s settings.py are the same credentials for pgAdmin.
settings.py
DATABASES = {‘default’: {‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,‘NAME’: env(‘DATABASE_NAME’),‘USER’: env(‘DATABASE_USER’),‘PASSWORD’: env(‘DATABASE_PASS’),}}
5. Generate a new secret key
I like using Djecrety to quickly generate secure secret keys.
I also use a .env file to secure my secret key and database credentials. To find out more, check out my post on the topic.
6. Rename the project
Rename the directory that contains settings.py. Do a find all and replace to rename all instances of the new project name.
7. Make your migrations
The only migrations that should appear in each of your app’s migrations folders are called ‘__init__.py’. As we have started a new database, we can delete any existing migrations and migrate from scratch.
In your terminal:
$ python manage.py makemigrations$ python manage.py migrate
8. Create a new superuser
python manage.py createsuperuser
9. Final checks
Start the development server and ensure everything is running without errors.
python manage.py runserver
That’s it! Now you’re ready to commit your changes to GitHub. Thanks for reading.