在Django中更改项目名称 [英] Changing a project name in django
问题描述
我使用Pycharm的重构>重命名将django项目的名称从 oldname
更改为 newname
。我在整个项目中进行了搜索,似乎到处都更改了名称。但是,当我尝试运行服务器时,这就是我得到的,
Traceback(最近一次通话):
File / &man; melissa / Dropbox / newname / manage.py,第15行,< module>
execute_from_command_line(sys.argv)
文件 /Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/core/management/__init__.py,第371行,在execute_from_command_line
utility.execute()
文件 /Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/core/management/__init__.py中,第317行,在执行
设置中。INSTALLED_APPS
文件 /Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py,第56行,在__getattr__
self._setup(name)
文件 /Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py中,行43,在_setup
self._wrapped = Settings(settings_module)
File /Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py中,第__行__中的第106行,
mod = importlib.import_module(self.SETTINGS_MODULE)
文件 /usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/ lib / python3.7 / importlib / __ init__.py,第127行,在import_module
中,返回_bootstrap._gcd_import(name [level:],包,级别)
文件< frozen importlib._bootstrap>,第1006行,在_gcd_import
文件< frozen importlib._bootstrap>,在_find_and_load
文件< frozen importlib._bootstrap>,行953,在_find_and_load_unlocked
文件< frozen中_call_with_frames_removed
文件中的 importlib._bootstrap>行中,在_gcd_import
文件中的< frozen importlib._bootstrap>文件,在_gcd_import
文件中,< frozen importlib._bootstrap>文件,第983行, _find_and_load
文件< frozen importlib._bootstrap>,行965,在_find_and_load_unlocked
ModuleNotFoundError中:没有名为 oldname的模块
有人可以帮我吗?
更改我的项目名称,问题是您正在使用
您可以看到Environment变量仍然具有以前的值,只需单击角落的图标以编辑 DJANGO_SETTINGS_MODULE
变量。
然后添加您的新项目名称 newname
,其中包含 settings.py
文件。
就是这样。
I changed the name of my django project from oldname
to newname
using Pycharm's refactor > rename. I have scoured through the project and it seems to have changed the name everywhere. But when I try runserver, here's what I get,
Traceback (most recent call last):
File "/Users/melissa/Dropbox/newname/manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'oldname'
Can someone help me with this?
I got the same error when changing my project name, the problem is that you're running the server using PyCharm, isn't it? Hopefully you created your project using PyCharm with Django Support. First of all add the below settings to your wsgi.py
and manage.py
files,
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldname.settings')
to
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'newname.settings')
Then go to your settings.py
and change the below settings,
ROOT_URLCONF = 'oldname.urls
WSGI_APPLICATION = 'oldname.wsgi.application'
to
ROOT_URLCONF = 'newname.urls
WSGI_APPLICATION = 'newname.wsgi.application'
If you didn't get PyCharm's Django Support, this solution should be performed otherwise you will get the same error again. You can try this by using some other terminal without getting PyCharm's Django Support and running your server with python manage.py runserver
command. If your project doesn't have any other errors you can see this solution is working. Then I realized something wrong with PyCharm's Django Support, the help that I have provided is only for who are using PyCharm's Django Support.
Go to your Project and click the Edit Configuration Settings.
You can see the Environment variable still has the previous values, just click the icon in the corner to edit the
DJANGO_SETTINGS_MODULE
variable.Then add your new project name
newname
, which contains thesettings.py
file.
That's it.
这篇关于在Django中更改项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!