Skip to content

Commit 59e05c0

Browse files
committed
fixes
1 parent 57e5bf3 commit 59e05c0

File tree

11 files changed

+153
-134
lines changed

11 files changed

+153
-134
lines changed

snooty.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ api = "https://django-mongodb.readthedocs.io/en/latest/"
1313
mdb-server = "MongoDB Server"
1414
django-version = "5.0"
1515
django-docs = "https://docs.djangoproject.com/en/{+django-version+}"
16+
django-api = "https://django-mongodb-backend.readthedocs.io/en/latest/"
17+
pymongo-version = "4.10"

source/get-started/create-app.txt

Lines changed: 15 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -43,45 +43,9 @@ To learn more about the ``sample_mflix`` database, see :atlas:`Sample Mflix Data
4343
Open the ``models.py`` file in the ``sample_mflix`` directory and replace
4444
its contents with the following code:
4545

46-
.. code-block:: python
47-
48-
from django.db import models
49-
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
50-
51-
class Award(models.Model):
52-
wins = models.IntegerField(default=0)
53-
nominations = models.IntegerField(default=0)
54-
text = models.CharField(max_length=100)
55-
56-
class Meta:
57-
managed = False
58-
59-
class Movie(models.Model):
60-
title = models.CharField(max_length=200)
61-
plot = models.TextField(blank=True)
62-
runtime = models.IntegerField(default=0)
63-
released = models.DateTimeField("release date", null=True, blank=True)
64-
awards = EmbeddedModelField(Award, null=True, blank=True)
65-
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
66-
67-
class Meta:
68-
db_table = "movies"
69-
managed = False
70-
71-
def __str__(self):
72-
return self.title
73-
74-
class Viewer(models.Model):
75-
name = models.CharField(max_length=100)
76-
email = models.CharField(max_length=200)
77-
password = models.CharField(max_length=100)
78-
79-
class Meta:
80-
db_table = "users"
81-
managed = False
82-
83-
def __str__(self):
84-
return self.name
46+
.. literalinclude:: /includes/get-started/models.py
47+
:language: python
48+
:copyable:
8549

8650
The ``Movie`` model represents the ``sample_mflix.movies`` collection
8751
and stores information about movies. This model contains an embedded
@@ -100,23 +64,9 @@ To learn more about the ``sample_mflix`` database, see :atlas:`Sample Mflix Data
10064
Open the ``views.py`` file in your ``sample_mflix`` directory and replace
10165
its contents with the following code:
10266

103-
.. code-block:: python
104-
105-
from django.http import HttpResponse
106-
from django.shortcuts import render
107-
108-
from .models import Movie, Viewer
109-
110-
def index(request):
111-
return HttpResponse("Hello, world. You're at the application index.")
112-
113-
def recent_movies(request):
114-
movies = Movie.objects.order_by("-released")[:5]
115-
return render(request, "recent_movies.html", {"movies": movies})
116-
117-
def viewers_list(request):
118-
viewers = Viewer.objects.order_by("name")[:10]
119-
return render(request, "viewers_list.html", {"viewers": viewers})
67+
.. literalinclude:: /includes/get-started/views.py
68+
:language: python
69+
:copyable:
12070

12171
These views display a landing page message and information about your ``Movie``
12272
and ``Viewer`` models.
@@ -127,18 +77,10 @@ To learn more about the ``sample_mflix`` database, see :atlas:`Sample Mflix Data
12777
To map the views defined in the preceding step to URLs, paste the following
12878
code into ``urls.py``:
12979

130-
.. code-block:: python
80+
.. literalinclude:: /includes/get-started/urls.py
81+
:language: python
82+
:copyable:
13183

132-
from django.urls import path
133-
134-
from . import views
135-
136-
urlpatterns = [
137-
path("", views.index, name="index"),
138-
path("recent_movies/", views.recent_movies, name="recent_movies"),
139-
path("viewers_list/", views.viewers_list, name="viewers_list"),
140-
]
141-
14284
Then, navigate to the ``quickstart/urls.py`` file and replace its contents with
14385
the following code:
14486

@@ -158,69 +100,18 @@ To learn more about the ``sample_mflix`` database, see :atlas:`Sample Mflix Data
158100
``templates``. Then, create a file called ``recent_movies.html``
159101
and paste the following code:
160102

161-
.. code-block:: html
162-
163-
<!-- templates/recent_movies.html -->
164-
<!DOCTYPE html>
165-
<html lang="en">
166-
<head>
167-
<meta charset="UTF-8">
168-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
169-
<title>Recent Movies</title>
170-
</head>
171-
<body>
172-
<h1>Five Most Recent Movies</h1>
173-
<ul>
174-
{% for movie in movies %}
175-
<li>
176-
<strong>{{ movie.title }}</strong> (Released: {{ movie.released }})
177-
</li>
178-
{% empty %}
179-
<li>No movies found.</li>
180-
{% endfor %}
181-
</ul>
182-
</body>
183-
</html>
103+
.. literalinclude:: /includes/get-started/recent_movies.html
104+
:language: html
105+
:copyable:
184106

185107
This template formats the movie data requested by the ``recent_movies`` view.
186108

187109
Create another file in the ``sample_mflix/templates`` directory called
188110
``viewers_list.html`` and paste the following code:
189111

190-
.. code-block:: html
191-
192-
<!-- templates/viewers_list.html -->
193-
<!DOCTYPE html>
194-
<html lang="en">
195-
<head>
196-
<meta charset="UTF-8">
197-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
198-
<title>Viewers List</title>
199-
</head>
200-
<body>
201-
<h1>Alphabetical Viewers List</h1>
202-
<table>
203-
<thead>
204-
<tr>
205-
<th>Name</th>
206-
<th>Email</th>
207-
</tr>
208-
</thead>
209-
<tbody>
210-
{% for viewer in viewers %}
211-
<tr>
212-
<td>{{ viewer.name }}</td>
213-
<td>{{ viewer.email }}</td>
214-
</tr>
215-
{% empty %}
216-
<tr>
217-
<td colspan="2">No viewer found.</td>
218-
</tr>
219-
{% endfor %}
220-
</tbody>
221-
</table>
222-
</body>
223-
</html>
112+
.. literalinclude:: /includes/get-started/viewers_list.html
113+
:language: html
114+
:copyable:
224115

225116
This template formats the user data requested by the ``viewers_list`` view.
226117

source/get-started/install.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ in your development environment.
6262

6363
This command also installs the following dependencies:
6464

65-
- PyMongo version {+version-number+} and its dependencies
66-
- Latest Django {+django-version-number+}.x version and its dependencies
65+
- PyMongo version {+pymongo-version+} and its dependencies
66+
- Latest Django {+django-version+}.x version and its dependencies
6767

6868
After you complete these steps, you have {+django-odm+} and its
6969
dependencies installed in your development environment.

source/get-started/query-data.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ to read data from the ``sample_mflix`` database.
2929
``sample_mflix.users`` collection for a movie viewer whose email is
3030
3131

32-
.. code-block:: python
33-
34-
from sample_mflix.models import Movie, Viewer
35-
36-
Viewer.objects.filter(email="[email protected]").first()
32+
.. literalinclude:: /includes/get-started/read-write-data.py
33+
:start-after: start-query-email
34+
:end-before: end-query-email
35+
:language: python
36+
:copyable:
3737

3838
This code returns the name of the matching user:
3939

@@ -48,9 +48,11 @@ to read data from the ``sample_mflix`` database.
4848
collection for movies that have a ``runtime`` value less than
4949
``10``:
5050

51-
.. code-block:: python
52-
53-
Movie.objects.filter(runtime__lt=10)
51+
.. literalinclude:: /includes/get-started/read-write-data.py
52+
:start-after: start-query-runtime
53+
:end-before: end-query-runtime
54+
:language: python
55+
:copyable:
5456

5557
This code returns a truncated list of the matching movies:
5658

291 KB
Loading

source/includes/get-started/models.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.db import models
2+
from django.conf import settings
3+
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
4+
5+
class Award(models.Model):
6+
wins = models.IntegerField(default=0)
7+
nominations = models.IntegerField(default=0)
8+
text = models.CharField(max_length=100)
9+
10+
class Meta:
11+
managed = False
12+
13+
class Movie(models.Model):
14+
title = models.CharField(max_length=200)
15+
plot = models.TextField(blank=True)
16+
runtime = models.IntegerField(default=0)
17+
released = models.DateTimeField("release date", null=True, blank=True)
18+
awards = EmbeddedModelField(Award, null=True, blank=True)
19+
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
20+
21+
class Meta:
22+
db_table = "movies"
23+
managed = False
24+
25+
def __str__(self):
26+
return self.title
27+
28+
class Viewer(settings.AUTH_USER_MODEL):
29+
name = models.CharField(max_length=100)
30+
email = models.CharField(max_length=200)
31+
password = models.CharField(max_length=100)
32+
33+
class Meta:
34+
db_table = "users"
35+
managed = False
36+
37+
def __str__(self):
38+
return self.name
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# start-query-email
2+
from sample_mflix.models import Movie, Viewer
3+
4+
Viewer.objects.filter(email="[email protected]").first()
5+
# end-query-email
6+
7+
# start-query-runtime
8+
Movie.objects.filter(runtime__lt=10)
9+
# end-query-runtime
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- templates/recent_movies.html -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Recent Movies</title>
8+
</head>
9+
<body>
10+
<h1>Five Most Recent Movies</h1>
11+
<ul>
12+
{% for movie in movies %}
13+
<li>
14+
<strong>{{ movie.title }}</strong> (Released: {{ movie.released }})
15+
</li>
16+
{% empty %}
17+
<li>No movies found.</li>
18+
{% endfor %}
19+
</ul>
20+
</body>
21+
</html>

source/includes/get-started/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.urls import path
2+
3+
from . import views
4+
5+
urlpatterns = [
6+
path("", views.index, name="index"),
7+
path("recent_movies/", views.recent_movies, name="recent_movies"),
8+
path("viewers_list/", views.viewers_list, name="viewers_list"),
9+
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!-- templates/viewers_list.html -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Viewers List</title>
8+
</head>
9+
<body>
10+
<h1>Alphabetical Viewers List</h1>
11+
<table>
12+
<thead>
13+
<tr>
14+
<th>Name</th>
15+
<th>Email</th>
16+
</tr>
17+
</thead>
18+
<tbody>
19+
{% for viewer in viewers %}
20+
<tr>
21+
<td>{{ viewer.name }}</td>
22+
<td>{{ viewer.email }}</td>
23+
</tr>
24+
{% empty %}
25+
<tr>
26+
<td colspan="2">No viewer found.</td>
27+
</tr>
28+
{% endfor %}
29+
</tbody>
30+
</table>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)