How to change name of your Django React project

Have you ever wondered how to easily change project name of your Django React stack?

I did.

Every time I start new project, I choose the first silly name, that comes to my mind.

But as time goes by, it can happen, that you completely change scope of the project and your project name become obsolete.

What was formerly known as “amazon_for_gardeners” could be “uber_for_craft_beer” next day.

In the early ages of the internet, you could easily rename your web project folder without any consequences.

But today, complexity of an average web app project grew thousandfold.

Before you can write a single line of code, you have to setup virtual environments, web frameworks, javascript compilers, CSS compilers, web servers, proxy servers, databases and git versioning.

It is no wonder, that something simple as renaming project folder can break your stack.

So I would like to share some tips on how I changed my Django React stack project folder name.

There are multiple caveats we have to keep in mind.

  • Django dependencies
  • React dependencies
  • Virtual environment dependencies
  • Database dependencies
  • File system dependencies
  • Git dependencies
  • Web server dependencies
  • Proxy server dependencies
  • Host configurations

 

Django

 

Lets’s start with Django project structure.

You can safely rename your Django project folder and Django app folder like so.

project_name/
         manage.py 
         project_name/ 
                 __init__.py 
                 settings.py 
                 urls.py 
                 wsgi.py

To.

new_project_name/
         manage.py 
         new_project_name/ 
                 __init__.py 
                 settings.py 
                 urls.py 
                 wsgi.py

Then rename some variables in settings.py, that is located at

/new_project_name/new_project_name/settings.py
ALLOWED_HOSTS = ['new_project_name.local', '127.0.0.1']

ROOT_URLCONF = 'new_project_name.urls'

WSGI_APPLICATION = 'new_project_name.wsgi.application'

STATIC_ROOT = '/new_project_name/new_project_name/static/'

MEDIA_ROOT = '/new_projecT_name/new_project_name/static/images/'

In wsgi.py change following lines.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new_project_name.settings')

In manage.py apply new project name too.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new_project_name.settings')

If you have Celery package installed, then don’t forget to change celery.py too.

/new_project_name/new_project_name/celery.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new_project_name.settings')

app = Celery('new_project_name’)

 

Gunicorn sever

 

Your Gunicorn sever needs treatment too.

In config file gunicorn.conf.py located at…

/new_project_name/gunicorn.conf.py

Change…

bind = "new_project_name.local:9010"

errorlog = '/new_project_name/new_project_name/logs/gunicorn-error.log'

accesslog = '/new_project_name/new_project_name/logs/gunicorn-access.log'

 

Nginx server

 

Find your Nginx conf file. On Mac, it is located at…

/usr/local/etc/nginx/nginx.conf

And change host variables to reflect your new project name.

server_name new_project_name.local;

access_log /new_project_name/new_project_name/logs/access.log;

/new_project_name/new_project_name/logs/error.log;

proxy_pass http://new_project_name.local:9010;

root /new_project_name/new_project_name/;

Don’t forget to restart Nginx afterwards.

sudo brew services restart nginx

 

SQLite database

 

In Django database, find table named django_site and change values in both “name” and “domain” column.

Or use this handy update select.

update django_site set name = "new_project_name.local", domain = "new_project_name.local;
commit;

 

Local Host

 

Find the configuration file for your local hosts.

On Mac, it is located at…

/private/etc/.host

There change your local host name variable.

new_project_name.local

 

Pipenv virtual environment

 

Previously, we renamed Django project folder, which is tightly tied to Pipenv virtual environment folder.

Unfortunately python virtual environments use hashed project folder name as a unique id.

Therefore we cannot simply rename virtual environment folder to reflect new project name.

If you try to run shell on new project name folder like so.

pipenv shell

Then Pipenv won’t recognise renamed virtual environment folder and will create new one with different unique hash.

New python virtual environment will lack any packages you have installed so far.

So reinstall all packages listed in pipfile with following command.

pipenv install

 

React and javascript stack

 

Lastly find package.json in your Django React stack.

Package.json is used for compiling your React code to static javascript file, that can be served by Django.

I have it located in my project here.

/new_project_name/package.json

There change following variables…

"scripts": {
    "dev": "webpack --mode development ./new_project_name/react/src/index.js --output ./new_project_name/react/static/frontend/main.js",
    "build": "webpack --mode production ./new_project_name/react/src/index.js --output ./new_project_name/react/static/react/main.js",
    "flush": "pipenv run python ./new_project_name/manage.py flush --no-input"
  },

Thats it!

Now everything should be fully functional and ready to go.

The only thing left is your Git repository.

But I have to still figure out how.

 

Update a day after: Renaming Git repository

So it turns out, that Git does not hold any references to project folder names.Β 

What I had to do was to go to Gitlab.com and in the settings, change project name and change Git repository url path.

Then if you are using Git GUI like GitKraken, just open new repo by choosing renamed project folder.

 

Hope this article helped you, so please share and comment if you liked it.

Seen you in the next post.

Share it!


Want to support me in my journey πŸš€? You can send me a donation below πŸ‘‡.

Donate with Paypal

Leave a Reply

avatar

Want to support me in my journey πŸš€? You can send me a donation below πŸ‘‡.

Donate with Paypal

Made with πŸ”₯ by Viet Phan 2018