Cloning Django appSometimes you may need to move your Django web application somewhere else. The first step is cloning the application from your GitHub repository, then you have to run a few commands from the range of installing the requirements to copying database data. Let us see how to do this.


Cloning repository

Create, then select the directory where you want to clone the repo.

Open your command-line interface or shell!

$ cd /set-your-path/

$ mkdir /your-directory-name/

$ cd /set-your-directory/

Type the git clone command with the repo's url and don't forget the dot at the end, except if you want to have an extra directory inside the current one!

$ git clone /your-repo-url/ .

Delete the .git file of the cloned repo. This is a hidden directory, so you have to set visibility in your file manager or type the next lines:

On Windows command prompt:

$ dir \a & rmdir \s .git

On PowerShell (I am using this too):

Get-ChildItem -Force;rm -r -fo .git

The ampersand in the windows command prompt separates two commands in one line, the semicolon does the same in the PowerShell command. With the first command you can see the content of the directory included hidden ones, and the second command is responsible for the deleting operation.

Creating virtual environment using pip.

$ pipenv shell

The next step is to install the required Python libraries and packages listed inside the requirements text file.

$ pipenv install -r requirements.txt

Postgres database & models

To Create new local Postgres database sign in the postgres database.

$ psql -U postgres

You are prompted to add your user password (user is postgres in this case).

Create local database to your cloned application. Don't forget the semicolon at the end!

$ postgres=# create database /your-db-name/ with owner postgres;

You can check the new database in the existing db list:

$ postgres=# \l

Exit from the psql console:

$ postgres=# \q

Open up your text editor or IDE (VS Code e.g.)!

If you have done your new local database, you have to add the new name to the DATABASES dictionary in your configuration file.

config/settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your-database-name', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'localhost', 'PORT': 5432 } }

Finally, you have to deal with your Django models.

Before you start, add an .env file to your project directory, and update it with the new database name and with the other necessary environment variables you have got in the cloned project. Generate a new unique SECRET_KEY and update the .env file with that too. Details here

You should now delete the old migration files in your Django apps. Be careful, and don't delete the empty __init__.py file in the migrations directory! If you don't want to delete the old migrations files for any reason, you should ignore the makemigrations command.

Return to your command-line interface or shell!

$ python manage.py makemigrations

$ python manage.py migrate

The server should run correctly. Check this:

$ python manage.py runserver

Dumpdata & loaddata

If you have any data in your database of the original app that you need for your new database, then you should go to the directory of the original app using the cd command.

Copy the data of the accounts model to a json file:

$ python manage.py dumpdata accounts > accounts.json --indent 4

Go back with the cd command to the directory of the new app, and copy the accounts.json to the this directory.

Then load the dumped data:

$ python manage.py loaddata accounts.json

If you get this error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte, update this file: lib/site-packages/django/core/serilazers/json.py in your virtualenv directory with adding 'utf-16' attribute to this: stream_or_string = stream_or_string.decode('utf-16') in the Deserializer() function.

Deploying to Heroku

First of all you should create a GitHub repository for your new project. Before you push your project to Heroku with this command:

$ git push heroku main

You should set the config vars for example:

$ heroku config:set SECRET_KEY=/secret-key/

Or you can set the config variables directly in the Heroku user interface: settings/config vars. You can do the dumpdata and loaddata commands in production mode as well, similar to what is seen above.