问题
I am trying to set up my first project in Django in the most comfortable way for me.
I really like to use console for such thing like :
- managing some project's settings ( before running for instance )
- version control
- creating project from a template
I know I with Django I can do such thing with :
django-admin startproject, manage.py runserver and probably even more
But for coding I prefer to use Eclipse than console ( vim :P ).
So I want to import that already created project into Eclipse.
Import project option doesn't recognize my Project's folder as a valid folder for importing ( probably, because it has no file like .eclipse_project in it ).
I can do it this way New->Project-> select my Project's folder as the content I want to start with.
This actually works, but the thing that annoys me is that I can't add even one single PyDev module to this project, because there are no source folders, even though I have one folder with module called __init__.py within. The project structure after executing this move is following :
mysite
manage.py
/mysite <- considered by PyDev not as source folder
__init__.py
urls.py
settings.py
wsgi.py
I could create source folder in the Project's root folder and then move the content of mysite/mysite into it, but :
- Won't I break something in Django project's structure?
- I don't want to do that everytime I create a django project?
- Is there an option to change type of that /mysite folder to source folder ?
Am I doing something wrong?
I also tried an option to create Django project using :
New->Django project-> I am getting the same structure, which either doesn't have source folder
btw. what is the recommended django project's structure ?
回答1:
I'm using Eclipse as my Django coding environment and you do not need a source folder. Just use the folder structure as it is provided by Django. The init.py shows python that this folder is actually a python module.
The command django-admin.py startproject myproject
will by default, create a new directory 'myproject' (=lets call it the project workspace folder) which contains a manage.py and a project package folder (also named 'myproject', containing a settings.py and other files).
You can rename the project workspace folder as you want, it does not change anything. Do not rename the project package (unless you know what you do), as the settings.py is referencing to this folder or exactly, is referencing to this module.
Having created your folder structure, go to Eclipse and create a new project selecting the project workspace folder, and you are ready.
This is a very lightweight setup, and you cannot run any django commands from Eclipse this way. Nevertheless I would recommend using the terminal / console for running Django commands, as it is very handy.
In order that Eclipse will be able to jump from one file to the other, using the command "cmd + mouseclick" on a method or similar, you have to include your project workspace folder into the system PYTHONPATH
in Eclipse. Just go to Eclipse - Preferences - PyDev - Interpreter Python
and add your folder via the button New Folder
. See screenshot below:

I can also recommend the Django text editor plugin for Eclipse.
回答2:
If you don't have source folder in Eclipse (for example after importing an existing project), you can right click on your project, choose Properties, choose PyDev - PYTHONPATH, and then add source folder. For example in your case it will be mysite folder.
Afterwards you can use PyDev options (PyDev Module, PyDev Package) options.
来源:https://stackoverflow.com/questions/12547097/django-project-console-management-coding-in-eclipse