Am using eclipse+pydev to build my django apps. I created a new workspace, built a new pydev project then created an empty folder in the new pydev project. In that empty folder i imported my old django application. Eclipse copied all the files & folders from my old django location to the new workspace.
I made the necessary changes in the settings.py on my new location, pointed the templates & data location to my new location workspace folder.
But when i run manage.py runserver from the new location workspace folder, django seems to point to my old folder location. All the html templates seems to point to the old location.
How can i make django execute stuff in my new location?
What am i missing?
Edit:
When i run the server inside eclipse, everything seems to be OK! funny.
Gath
There is no reason why you could not move your django folder, except for the presence of some absolute file path that you added. You should never have an absolute path in your project.
You should use the following trick in the settings.py file, as explained in the django wiki:
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# then for each subdirectory:
SOME_DIR = os.path.join(BASE_DIR, 'my_dir')
Then you can move your whole project without ever changing a single character inside.
I agree that something seems odd. Not hardcoding your paths is of course the first place to start. One other possible cause of the error could be that you have added your original project/app to the PYTHONPATH and that it gets imported from there instead of the new place.
If being on a *NIX-system you could symlink your old location to the Eclipse workspace.
Related
After a long time away from an app i wrote in Django and didn't complete, I've come back to it on a new Mac.
I'm struggling to get the code to refer to the apps and the files within them without the explicit path. For instance:
from myproject.app.file import object
Whereas I remember not having to use myproject every time.
Is this something that has changed? I seem to remember being about to add to the path in manage.py which is called every time you run the dev server, but this hasn't worked this time.
sys.path.append /path/to/myproject
Should that fix the issue I'm having?
I started with a simple answer and it grew into more details on how to add subdirectories of your project to python path. Maybe a bit off-topic, but it could be useful to you so I'm pushing the post button anyway.
I usually have a bunch of small re-usable apps of mine I keep inside my project tree, because I don't want them to grow into independent modules. My projet tree will look like this:
manage.py
myproject/apps
myproject/libs
myproject/settings
...
Still, by default, Django only adds the project root to python path. Yet it makes no sense in my opinion to have apps load modules with full path:
from myproject.apps.author.models import Author
from myproject.libs.rest_filters import filters
That's both way too verbose, and it breaks reusability as I only use absolute imports. Not to mention if I someday build an actual python package out of some of the libs, it will break.
So, I took the following steps. I added the relevant folders to the path:
# in manage.py
root = os.path.dirname(__file__)
sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'apps')))
sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'libs')))
But you must ensure those packages cannot be loaded from the root of the project, or you will have odd issues as python would load another copy of the module. For instance, isinstance(libs.foo.bar(), myproject.libs.foo.bar) == False
It's not hard though : just remove __init__.py from the folders you add to the path. That will make sure they cannot be descended into from the project.
Also, Django's discover runner will not descend into those paths unless you specify them manually. That may be fine with you (if every module has its own test suite). Or you can extend the runner, so it knows about this: sample code.
Is there an easy way to rename a project? I tried to rename the folder, but it didn't work.
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:
Rename the oldprojectname directory to newprojectname
manage.py: Change os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldprojectname.settings')
newprojectname/wsgi.py: Change os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldprojectname.settings')
newprojectname/settings.py: Change ROOT_URLCONF = 'oldprojectname.urls' and change WSGI_APPLICATION = 'oldprojectname.wsgi.application'
newprojectname/urls.py: Change oldprojectname in a line I had added
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.
To rename a Django project, you need to change the project name wherever it appears.
First of all, rename both outer and inner project directory name from old_project_name to new_project_name
So if the project directory looks like this :
old_project_name // outer project directory old name
old_project_name// inner project directory old name
--__init__.py
--asgi.py
--settings.py
--urls.py
--wsgi.py
app_name // any app u created in the project
db.sqlite3
manage.py
then change to this :
new_project_name // outer project directory new name
new_project_name// inner project directory new name
--__init__.py
--asgi.py
--settings.py
--urls.py
--wsgi.py
app_name // any app u created in the project
db.sqlite3
manage.py
In 2021 with Django version 3.2.3, there are in total 9 places (including comments and codes both) in 5 files where the name of the project appears, which are :
In new_project_name/asgi.py file (comment) :
ASGI config for old_project_name project.
In new_project_name/asgi.py file (code) :
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'old_project_name.settings')
In manage.py file (code) :
os.environ.setdefault('DJANGO_SETTINGS_MODULE', old_project_name.settings')
In new_project_name/settings.py file (comment) :
Django settings for old_project_name project.
In new_project_name/settings.py file (code) :
ROOT_URLCONF = 'old_project_name.urls'
In new_project_name/settings.py (code) :
WSGI_APPLICATION = 'old_project_name.wsgi.application'
In new_project_name/urls.py file (comment) :
old_project_name URL Configuration
In new_project_name/wsgi.py file (comment) :
WSGI config for old_project_name project.
In new_project_name/wsgi.py file (code) :
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'old_project_name.settings')
Note : To change the old_project_name to new_project_name in the files you can use your IDE 's/ text editor's find & replace function to avoid any errors.
Tip : If you haven't done much work in the project then its better to create a new Django project and then just copy and paste the code which don't need any changes rather than changing project name.
I think the best solution here is to simply open your settings.py, urls.py, views.py and any other file that might rely on your project's name and then use the find&replace function in your text editor.
Or, if you haven't done much work yet, start a new project with django-admin.py and copy/paste.
As a beginner with Python and Django, the below simple steps worked for me.
Update both outer and inner folder names in your project (directly from windows explorer or from any editor like visual studio)
Search and replace your old project name with new one in below project files
a. manage.py
b. settings.py
c. wsgi.py
d.asgi.py
e. urls.py
Restart server and confirm if everything works fine again
Actually what you have to do is this:
Open your project in any code editor like Sublime or VSCode
Search for your current project name
In the replace box enter the new project folder name that you want.
Now the code editor will automatically search in the whole project folder and will replace the current project name with the new name.
Now just rename the main app name which is in your project folder
You can now rename your project folder name
Thats it. Hope that helps :)
Suppose your project name is old_project_name and you want to change it to new_project_name. Also you have created an app called my_app within old_project_name. Your directory structure will be something like -
old_project_name
--old_project_name
--__init__.py
--settings.py
--urls.py
--wsgi.py
my_app
--Files and folders under my_app
db.sqlite3
manage.py
Rename both inner and outer old_project_name directory name to new_project_name. After changing your directory structure will be as something like -
new_project_name
--new_project_name
--__init__.py
--settings.py
--urls.py
--wsgi.py
my_app
--Files and folders under my_app
db.sqlite3
manage.py
Change reference to old_project_name in your project files to new_project_name. Mostly you will need to change the reference in settings.py, wsgi.py and manage.py
After these changes run local server and check if your routes are working fine.
Change git repository name. This is optional, but it is advisable as it will be easy to track your projects. If you have added your project to bitbucket or github, then login into bitbucket or github and change the repository name to new_project_name. Suppose your old repository url is https://<yourusername>#bitbucket.org/<yourusername>/old_project_name.git
After renaming repository, your project git url will be changed to something like https://<yourusername>#bitbucket.org/<yourusername>/new_project_name.git>
Run in terminal
git remote -v
it will list your current remote repository url
origin https://<yourusername>#bitbucket.org/<yourusername>/old_project_name.git(fetch)
origin https://<yourusername>#bitbucket.org/<yourusername>/old_project_name.git(push)
Run following command in terminal. This will change your current remote repository url
git remote set-url origin https://<yourusername>#bitbucket.org/<yourusername>/new_project_name.git
Run in terminal
git remote -v
It will now list something like
origin https://<yourusername>#bitbucket.org/<yourusername>/new_project_name.git(fetch)
origin https://<yourusername>#bitbucket.org/<yourusername>/new_project_name.git(push)
Now you can push your new updates to your remote repository.
Update virtual environment. This is optional,but it will help to identify virtual environ mapping to project. I use virtualenvwrapper. If you are using a different one, you will have to update the commands as per your virtual environment.
Run following command in terminal. This will create a copy of old_project_name environment setting with name new_project_name.
cpvirtualenv old_project_name new_project_name
Remove old environment. This step is optional as well.
Run following command in terminal
rmvirtualenv old_project_name
I have a pycharm project and, presumably, a Django project. Perhaps they are one and the same, perhaps not - I'm unsure as to the distinction.
Anyway, in my settings.py file (which is in the root of project directory, which is what I presume is my pycharm project) I have:
ROOT_URLCONF = 'dumpstown.urls'
Does that mean dumpstown is my project name? Or my pycharm project name? What is the distinction? Because I also have a folder called dumpstownapp and this has all my models.py and view.py files. I would have thought that dumpstownapp was the Django project, but I really don't know!
So, to be concise:
In this folder setup
folderA
folderB
views.py
models.py
<other stuff>
templates folder
settings.py
<other stuff>
which is the "Django project name" ~ and by that I mean, if I have a UserProfile defined in my models.py (shown above) what would be the AUTH_PROFILE_MODULE entry I'd need for it? I'm getting several understandings from the django docs - I'd assume
dumpstownapp.models.UserProfile
But from the docs I'd get
dumpstownapp.UserProfile
Or maybe my app is called dumpstown? and then what do I get?
FolderA is the Django project folder, and folderB is the Django app folder.
I haven't used a UserProfile, but according to the docs ( https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users) it should contain a string containing the Django app name and the model name separated by a dot, so "dumpstownapp.UserProfile" should be correct. If you show the error someone can probably give you better help..
Django documentation used to say that the parent of the project folder (i.e. the parent of folderA) should be on the path, but I believe that has been changed to also include the project folder itself (i.e. folderA) -- since it makes sharing of Django apps much easier. PyCharm seems to assume that is the case, since pressing Alt+F7 to auto-add an import for a newly used module create an import statement that assumes folderA is on the import path (I'm a relative newcomer to PyCharm, and I'm using it on a project that started in the Django 0.96 era, so I might just have things set up wrong..) But folderA is both the Django and the PyCharm project (the .idea file is where PyCharm stores its project data).
In one of my django-app git-submodule projects I needed to find out the name of the Django project that django-app/library was used in. To that end, I tried to get the path of the file that was being executed, find my package in the path and take the folder above it. However, it turned out that on the production server the project was deployed in a folder with a different name (a standard name like www or something along those lines). So this way is not fully reliable.
So I ended up setting a PROJECT variable in the django settings file and using that instead.
I was trying to use a template but it keeps giving me a TemplateDoesNotExist exception. If I look at the Template-loader postmortem, I notice that it is looking in the wrong directory. It is trying to find templates within my workspace folder and not within the location of the project (outside workspace)
If I try to specify a path in settings, it just appends that path to the workspace path.
I'm quite new to Eclipse and PyDev -- how can I sort this out?
I'm using Eclipse 3.7.2 with PyDev 2.4.0 and Django 1.4
This is what I do:
At the top of your file, write these two lines:
import os
ROOT_PATH = os.path.dirname(__file__)
Change TEMPLATE_DIRS to be:
os.path.join(ROOT_PATH, 'templates'),
The 'templates' part should be the relative path to your settings file. In my case it is just templates.
You can also use ROOT_PATH anywhere in the settings, along with join, and it's not a bad idea to use it, because this way your settings file is more universal then constrained to the same machine.
In your settings.py file, there should be a TEMPLATE_DIRS directive. This is a tuple which allows you to set up multiple directories (see below) or just a single entry, as is usually the case. Set that to the appropriate folder and it should work. If that doesn't check permissions on the folder to ensure the runtime user has access to it and the templates.
TEMPLATE_DIRS = (
'/var/www/templates/',
'/opt/myweb/django/templates',
)
It's not so easy questions as others.. at least. it's not a problem to setup simple one-dir-based static files location..
https://bitbucket.org/sirex/django-starter/src
There's such an interesting project in here.. this one is using distribute and buildout for making whole project and django with modules in one dir. you can migrate from dev to production mode easy and etc.. all you need is just to rename dir, and type "make" in it, and that's it =) there's manual in there...
Situation which works with python server, and don't work with apache mod_wsgi:
Default static files location is: "var/htdocs/static". This can be overridden with one static dir location for example apps/myapp/myapp/static/. This works with python webserver but doesn't work with wsgi/apache. wsgi can't see anything apart default directory.. example: http://localhost:8000/static/css/main.css works but with apache same url doesn't work. and this file lies in myproject/apps/myapp/myapp/static/css/main.css although default static dir is var/htdocs/static =)
As far as I understand this overriding made with StaticFiles application in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'htdocs', 'static')
STATICFILES_DIRS = (
os.path.join(BUILDOUT_DIR, 'project', 'static'), # <-- why "project" and not "apps" I don't know X_X
)
maybe this one is incorrect, I don't know but with py-server this works. apache vhost works with default location.. and setuped to "var/htdocs/static".
Maybe problem is in wsgi script?
#!/usr/local/bin/python2.6
import os,sys
sys.path[0:0] = [
'/usr/local/lib/python2.6/site-packages/',
'/www/webapp/visimes/eggs/PIL-1.1.7-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/South-0.7.3-py2.6.egg',
'/www/webapp/visimes/eggs/django_annoying-0.7.6-py2.6.egg',
'/www/webapp/visimes/eggs/coverage-3.4-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/django_debug_toolbar-0.8.4-py2.6.egg',
'/www/webapp/visimes/eggs/django_extensions-0.6-py2.6.egg',
'/www/webapp/visimes/eggs/django_test_utils-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipdb-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipython-0.10.1-py2.6.egg',
'/www/webapp/visimes/eggs/djangorecipe-0.21-py2.6.egg',
'/www/webapp/visimes/eggs/zc.recipe.egg-1.3.2-py2.6.egg',
'/www/webapp/visimes/eggs/zc.buildout-1.5.2-py2.6.egg',
'/www/webapp/visimes/eggs/BeautifulSoup-3.2.0-py2.6.egg',
'/www/webapp/visimes/eggs/setuptools-0.6c12dev_r88795-py2.6.egg',
'/www/webapp/visimes/parts/django',
'/www/webapp/visimes',
'/www/webapp/visimes/project', # <-- this one need for monitor.py which i put in there
'/www/webapp/visimes/apps/portal', # <-- startapp.sh script some how forgot to add this dir, it's my default app dir, which must be generated with startapp.sh and added in here..
]
import djangorecipe.wsgi
if __name__ == '__main__':
djangorecipe.manage.main('project.development')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.development'
import monitor
monitor.start(interval=1.0)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I added last 4 lines by my self. because I couldn't start apache.. I'm guessing that djangorecipe.wsgi should handle everything else with staticFile override.. anyway please, check out that package, if you on linux or mac, and try it by your self. it must work
ps. (btw bin/django need to dublicated as bin/django.wsgi and etc/apache.conf is generated vhost for apache)
I'd really apriciated if somebody would try to launch this "Starter" manually with wsgi... then you'd understand everything.=)
Edit: Any information about how can WSGI understand where he needs to search static files apart default location from django settings, is REALLY appreciated =)
There is lots of documentation on the official mod_wsgi site for understanding how to use it. This includes how to set it up for serving static media files. See:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
WSGI has nothing whatever to do with serving static files. This is all clearly covered in the Django deployment documentation - as Torsten suggests in the comments, you'll need to point Apache at your static files, probably via an alias.
I must say though that this project looks very dodgy. Manually adding a load of eggs to sys.path is not the right way to go about things - a much better way would be to use something like virtualenv, which manages all that for you.
No way of having some kind of overriding like django does with WSGI..
there's great command in django "manage.py collectstatic" which places all files from STATIC_DIR list (in settings.py) to main static directory.. in fact this commands just copy files from all those dir's and that's it =)
Would be great to know, how could I make this copying automatic when any file in that dir would be updated.. same thing like monitor.py for automatic wsgi reload when source is modified...