Create an Application
In your quickstart
project, you can create an application
that interacts with the Atlas sample database called sample_mflix
.
This database contains a movies
collection, which stores
information about movies. The database also contains a users
collection, which stores information about movie viewers who use
a streaming service.
To learn more about the sample_mflix
database, see Sample Mflix Dataset in the Atlas documentation.
Create a "sample_mflix" app
From your project's root directory, run the following command to create a
new Django app called sample_mflix
based on a custom template:
python manage.py startapp sample_mflix --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.0.x.zip
Note
App Template
The django-mongodb-app
template ensures that your app.py
file
includes the line "default_auto_field = 'django_mongodb_backend.fields.ObjectIdAutoField'"
.
Create models for movie, awards, and viewer data
Open the models.py
file in the sample_mflix
directory and replace
its contents with the following code:
from django.db import models from django.conf import settings from django_mongodb_backend.fields import EmbeddedModelField, ArrayField from django_mongodb_backend.models import EmbeddedModel class Award(EmbeddedModel): wins = models.IntegerField(default=0) nominations = models.IntegerField(default=0) text = models.CharField(max_length=100) class Meta: managed = False class Movie(models.Model): title = models.CharField(max_length=200) plot = models.TextField(blank=True) runtime = models.IntegerField(default=0) released = models.DateTimeField("release date", null=True, blank=True) awards = EmbeddedModelField(Award, null=True, blank=True) genres = ArrayField(models.CharField(max_length=100), null=True, blank=True) class Meta: db_table = "movies" managed = False def __str__(self): return self.title class Viewer(models.Model): name = models.CharField(max_length=100) email = models.CharField(max_length=200) class Meta: db_table = "users" managed = False def __str__(self): return self.name
The Movie
model represents the sample_mflix.movies
collection
and stores information about movies. This model contains an embedded
model field named awards
, which stores an Award
object. The
model also contains an array field named genres
, which stores
a list of genres that describe the movie.
The Award
model does not represent a separate collection. Instead, it
represents the embedded document values stored in the Movie
model.
The Viewer
model represents the sample_mflix.users
collection
and stores account information for movie viewers.
Create views to display data
Open the views.py
file in your sample_mflix
directory and replace
its contents with the following code:
from django.http import HttpResponse from django.shortcuts import render from .models import Movie, Viewer def index(request): return HttpResponse("Hello, world. You're at the application index.") def recent_movies(request): movies = Movie.objects.order_by("-released")[:5] return render(request, "recent_movies.html", {"movies": movies}) def viewers_list(request): viewers = Viewer.objects.order_by("name")[:10] return render(request, "viewers_list.html", {"viewers": viewers})
These views display a landing page message and information about your Movie
and Viewer
models.
Configure URLs for your views
Create a new file called urls.py
file in your sample_mflix
directory.
To map the views defined in the preceding step to URLs, paste the following
code into urls.py
:
from django.urls import path from . import views urlpatterns = [ path("recent_movies/", views.recent_movies, name="recent_movies"), path("viewers_list/", views.viewers_list, name="viewers_list"), path("", views.index, name="index"), ]
Then, navigate to the quickstart/urls.py
file and replace its contents with
the following code:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path("admin/", admin.site.urls), path("", include("sample_mflix.urls")), ]
Create templates to format your data
In your sample_mflix
directory, create a subdirectory called
templates
. Then, create a file called recent_movies.html
and paste the following code:
<!-- templates/recent_movies.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Recent Movies</title> </head> <body> <h1>Five Most Recent Movies</h1> <ul> {% for movie in movies %} <li> <strong>{{ movie.title }}</strong> (Released: {{ movie.released }}) </li> {% empty %} <li>No movies found.</li> {% endfor %} </ul> </body> </html>
This template formats the movie data requested by the recent_movies
view.
Create another file in the sample_mflix/templates
directory called
viewers_list.html
and paste the following code:
<!-- templates/viewers_list.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Viewers List</title> </head> <body> <h1>Alphabetical Viewers List</h1> <table> <thead> <tr> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> {% for viewer in viewers %} <tr> <td>{{ viewer.name }}</td> <td>{{ viewer.email }}</td> </tr> {% empty %} <tr> <td colspan="2">No viewer found.</td> </tr> {% endfor %} </tbody> </table> </body> </html>
This template formats the user data requested by the viewers_list
view.
Include your app in your project
Open the settings.py
file in quickstart
and edit your
INSTALLED_APPS
setting to resemble the following code:
INSTALLED_APPS = [ 'sample_mflix.apps.SampleMflixConfig', 'quickstart.apps.MongoAdminConfig', 'quickstart.apps.MongoAuthConfig', 'quickstart.apps.MongoContentTypesConfig', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
After completing these steps, you have a basic Django MongoDB Backend app that
you can use to access the sample_mflix
Atlas database.