I tried two ways to do code completion, one is OK, the other fails.
OK one does like below:
$> cd myDjangoProject/
myDjangoProject $> export PYTHONPATH="."
myDjangoProject $> DJANGO_SETTINGS_MODULE=settings vim urls.py
Then ^x ^o can work well. But this method leads me repeatly do above when edit a file in project.
So an idea comes to me, why not create a script to do above automatically?
Refer to Blog for django code completion in vim, this is exactly what I think, but I encounter a problem during my configuration.
Fail one below:
-
create a script in
/usr/binnamedvim_wrapper#!/bin/bashexport PYTHONPATH="${PYTHONPATH}:/path/to/myDjangoProject/"DJANGO_SETTINGS_MODULE="/path/to/myDjangoProject/settings" vim $@ -
Add alias in
~/.bashrcalias vi="vim_wrapper"
Restart terminal sesstion, command vi /path/to/myDjangoProject/urls.py, make a test :python from django import db, an Error happen says:
ImportError: Could not import settings
‘myDjangoProject/settings’ (Is it on
sys.path?): Import by filename i s not
supported.
I don’t know how to solve this. Thanks for help.
Create the
vim_djangoexecutable script in/usr/binContent in
vim_djangoscript:#!/bin/bashType
vim_django urls.py(or other files)to edit in django project,Ctrl-x & Ctrl-ofor code completion.NOTE: In
PROJECTsettings, you may noticeSolutionsthat is parent directory of all my django projects