Python project for Makers AceBook
Authors
- Neil
- Preeti
- David
- Sundar
After creating virtual env ensure you have the following installed
-
BDD
- mamba
- expects
-
Web Framework
- Django
-
Django ORM for MongoDB
- Djongo
- MongoDB
If virtual enviroment is required perform below steps,
Virtual enviroment after project dir exists. 'env' in the command below can be any directory. It gets created if it does not exist already
virtualenv -p /usr/local/bin/python3 env
source env/bin/activate
To install perform the below steps,
Activate your virtual environment and issue the below from within the virtual environment
pip install mongodb
Make
db
directory structure in project folder,
cd pybook directory
mkdir -p ./db
Running MongoDB in background, use
ps -eaf | grep mongo
(this gets process id to kill if needed and start it) start Daemon specifying DB path location, open a shell and type. Using an ampersand at the end will ensure the deamon runs in the background and the terminal can be reused if need be
mongod --dbpath ./db &
Start MongoDB command line, open another shell and type
> mongo
> use pybook_test
> db.createCollection("users")
> show collections
> db.users.insert({ user_name: "joe bloggs", email: "joe.bloggs@makers.com", user_message: "Hello World!"})
> db.users.find()
Note Django follows the Model-Template-View frameork, application gets data from the model, view uses the data and then renders web template containing processed information, Django views has likeness of controllers akin to MVC.
- Installing Django
pip install django
- Installing Djongo
pip install djongo
- Installing BDDs
pip install mamba
pip install expects
- Installing MongoDB
pip install mongodb
mongod --dbpath ./db &
note db is a folder you created inside your project path
- Running Django
from inside the folder that has migrate.py.
python manage.py migrations
python manage.py runserver
if you see errors or text in red after running server, stop the process and ensure the missing libraries/packages or steps are followed before attempting to access the page
- Create app using
./manage.py startapp msg_board
- In the settings.py of the pybookapp folder update the following
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'pybook_test1',
}
}
At this stage from the pybookapp folder run
./migrate.py makemigrations
and ./manage.py migrate
- In the urls.py of the pybookapp update the following - see the second line to include the msg_board.urls
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^', include('msg_board.urls')),
]
- In the msg_board folder update the models.py and include the following
from djongo import models
and create a model. Django model is the MongoDB's representation of collection (table). The Model created through Django in its plural form become the collection in MongoDB for example Model Player becomes players collection. If Django were to manage your DB the collections will be created in the following format containername_appname_modelS pybookapp_msg_board_players
class Player(models.Model):
user_name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
user_message = models.CharField(max_length=100)
objects = models.DjongoManager()
def __str__(self):
return self.user_name
- Next step is to register your model in the admin.py of the msg_board, hence update it as follows
admin.site.register([Player])
- Update your app.py under msg_board with the following
class MsgBoardConfig(AppConfig):
name = 'msg_board'
- Update your views in the msg_board with the following changes
include the views
from msg_board.models import Player
and update the following
class UserListProperView(TemplateView):
def get(self, request, **kwargs):
print("Fetching users via Djongo")
player = Player.objects.all()
print(player)
print(len(player))
print(dir(player))
data = {}
data['object_list'] = player
return render(request, 'player.html', {"data": data})
- finally in your urls.py of the msg_board make the following changes
include the views
from msg_board import views
and update the url patterns
urlpatterns = [
url(r'^$', views.HomePageView.as_view()),
url(r'^user', views.UserListView.as_view()),
url(r'^player', views.UserListProperView.as_view()),
]
Setting up Django,
> For the first time
django-admin startproject helloapp
once done direcotry structure will look like below,
helloapp
├─helloapp
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
helloapp and manage.py are the main project folder, settings sre in settigns.py and routes are in urls.py.
Create new by cd into helloapp directory and type > python manage.py startapp myapp, it will create folder within called myapp
Django will only regonise new application if addded to settign file, under INSTALLED APPS.
To run Django, type python manage.py runserver, then access via [localhost](http://localhost:8000)