Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to rename a django project

Tags:

django

I would like to know if there is an easy way to rename a project. The project has an app that I would like to also rename. Of course if I rename it the folder, it won't work. I did a search in here and Google and I could not find the answer. Thanks

like image 904
amb1s1 Avatar asked Aug 17 '13 22:08

amb1s1


People also ask

How do you rename a project in Python?

Renaming ProjectsRight-click the root folder of your project and select Refactor | Rename from the context menu or press Shift+F6 . In the dialog that opens, choose the rename strategy. If the project name is the same as the name of its root folder, select Rename directory.

What is the command to start a new Django project called my project?

$ django-admin startproject myproject . This can be a stumbling block because it is not documented in Django's official tutorial. The “django-admin help startproject” command does document the optional directory argument but does not explain when this option is useful.


2 Answers

Renaming the project is actually easier than renaming an app. This question explains how to rename an app.

To rename the project, you need to change the project name wherever it appears. grep -nir oldname . can help you find where it appears. In my case, I had to change the following places:

  1. Rename the oldprojectname directory to newprojectname

  2. manage.py: Change os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldprojectname.settings')

  3. newprojectname/wsgi.py: Change os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldprojectname.settings')

  4. newprojectname/settings.py: Change ROOT_URLCONF = 'oldprojectname.urls' and change WSGI_APPLICATION = 'oldprojectname.wsgi.application'

  5. newprojectname/urls.py: Change oldprojectname in a line I had added

like image 70
krubo Avatar answered Sep 23 '22 01:09

krubo


very simple and efficient

add this command to any app in your project like this ,

# app/management/commands/renameproject.py  import os import glob from django.conf import settings from django.core.management.base import BaseCommand, CommandError   class Command(BaseCommand):     help = 'Renames the Project'      def add_arguments(self, parser):         parser.add_argument('old', nargs='+', type=str, help="current project name")         parser.add_argument('new', nargs='+', type=str, help="new project name")      def handle(self, *args, **options):         old = options["old"][0]         new = options["new"][0]          base = str(settings.BASE_DIR)         projectfiles = []         managefile = os.path.join(base, "manage.py")         projectfiles.append(managefile)         projectfiles += glob.glob(os.path.join(base, old, "*.py"))         projectfiles += glob.glob(os.path.join(base, old, "**", "*.py"))         for pythonfile in projectfiles:             with open(pythonfile, 'r') as file:                 filedata = file.read()              filedata = filedata.replace(old, new)              with open(pythonfile, 'w') as file:                 file.write(filedata)         os.rename(os.path.join(base, old), os.path.join(base, new)) 

Now just run this command

python manage.py renameproject oldname newname  

have fun 😎

How it works:

Searches .py files across the project and replaces the old name with new.

like image 43
Al Mahdi Avatar answered Sep 23 '22 01:09

Al Mahdi