xnjx1a
Last Updated: October 19, 2017
·
5.712K
· guangyi
Cfccdf00acb76649add0727077e3a1c7

Study note: Install and setup Djang_MongoDB_Engine

As a background, I just begin to study MongoDB and have a little knowledge of Django.
This is more like a study note to help me remember how did I finish every step.

I use Django + MongoDB +Django MongoDB Engine
And <a href="http://django-mongodb-engine.readthedocs.org/en/latest/tutorial.html">this is the tutorial I followed to create a single user blog application.</a> But after I began to setup I find this bloc can only cover the installation and setup part...

To install and setup MongoDB, I use <a href="http://www.mkyong.com/mongodb/how-to-install-mongodb-on-mac-os-x/"> this tutorial</a>. I think it's detailed than the official one

1. install pip

I don't know why I didn't install it before....Here is the <a href="http://www.pip-installer.org/en/latest/installing.html">steps </a>I followed to install pip

But I have to add "sudo" in front of the commands to get permission

Below are some very important steps...I messed up at first, and it took me a very long time to find out why! It almost drive me crazy...

2.install Django MongoDB Engine:

Following the <a href="http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html">setup tutorial</a>

a: install virtualenv

$sudo pip install virtualenv

b: I'd like to create Django project first. Because we will use it later.
Change into the target directory, and input

$django-admin.py startproject myproject

This will create a folder called "myproject" and under that folder there are some python files. One of them is called settings.py, another is manage.py.

DO NOT skip step c and d to install the following python packages! Skipping those two step was a mistake I made which made the setup of DjangoMongoDBEngine so annoying....I had no idea what is virtualenv when I began to install the Engine, now I have better idea what it's for.

c:Set up a virtual environment for "myproject",

Not inside "myproject" folder, but in the container of "myproject" folder, use:

$virtualenv myproject

This will initialize the myproject folder with python site-package and something essential.
It will create /bin, /lib etc... folders.

d:Activate it use:

$source myproject/bin/activate

e: Make sure the virtual environment is active now! and then install Djang_nonrel and toolbox and that engine.

$sudo pip install git+https://github.com/django-nonrel/django@nonrel-1.3

$sudo pip install -U git+https://github.com/django-nonrel/djangotoolbox/@toolbox-1.3

$pip install git+https://github.com/django-nonrel/mongodb-engine@mongodb-engine-1.3

3.Set up Django database:

In the settings.py(/myproject/settings.py) find DATABASES, and do the changes as below.

DATABASES = {

'default' : {

      'ENGINE' : 'django_mongodb_engine',

      'NAME' : 'my_database'

   }

}

4.Create my_database

in the terminal,to start the MongoDB server input

$mongod

to start the database shell in another terminal window, input

$mongo 

in the database shell, to create and switch to my_database input:

use my_database

5.Test it:

under the "myproject" folder, where manage.py locates, try

$python manage.py runserver

If you see this:

Validating models...
0 errors found
Django version 1.3.7, using settings 'myproject.settings'
Development server is running at http://127.0.0.1:8080/

Hurray! it success!

But you may also see some other errors like what i have experienced...Frustration.

1.If you see the error message, like 'djangomongodbengine' isn't an available database backend.. No module named utils" .

It may caused by not activating the environment. or if you do activate it, but still see this message, then the toolbox version is not correct. Make sure its version is 1.3. After I reinstall toolbox, it works!

2.You may also see this: "ImportError: No module named core.management"

In my case, while activate the virtual environment, I will see this message. Go to /myproject/lib/python2.7/site-packages (you may use different python version ) if you couldn't find django, djangomongodbengine, djangotoolbox, that probably means when you install these packages, you are not activate the virtual environment! That's the mistake i made during the setuo. I also can't import django while in this virtual environment.

But probably you have other reasons other than mine, <a href="http://stackoverflow.com/questions/6049933/django-import-error-no-module-named-core-management">this is a related stack overflow questions and answers</a> I think may helpful.

BTW, the first line in my manage.py is

#!/usr/bin/env python

I didn't change it.

These are almost every step and problems I encountered during the setup.

If you are newbie like me, I hope this is helpful~

I need to take a break now~~~~

Say Thanks
Respond

1 Response
Add your response

29367

I came across your great presentation on using Django and MongoDB. At the same time I am wondering if you are aware of djongo

It translates a SQL query string into a mongoDB query document. I can use the original django ORM without having to install a new ODM to get Django to work!

I wonder why you feel using MongoEngine is a better alternative. I am able to use all contrib modules like admin, using this connector. There is no additional effort of learning a new ODM either with this connector.

about 2 months ago ·