Skip to content

Commit 1caf1a3

Browse files
committed
more info
1 parent 704c54e commit 1caf1a3

File tree

2 files changed

+183
-36
lines changed

2 files changed

+183
-36
lines changed

source/django-get-started/django-create-app.txt

Lines changed: 128 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,77 @@ Create an Application
2828
The ``django-mongodb-app`` template ensures that your ``app.py`` file
2929
includes the line ``"default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'"``.
3030

31-
.. step:: Create a view
31+
.. step:: Create models for movie and user data
32+
33+
Open the ``models.py`` file in the ``sample_mflix`` directory and replace
34+
its contents with the following code:
35+
36+
.. code-block:: python
37+
38+
from django.db import models
39+
40+
class Movies(models.Model):
41+
title = models.CharField(max_length=200)
42+
plot = models.TextField(null=True)
43+
runtime = models.IntegerField(default=0)
44+
released = models.DateTimeField("release date", null=True)
45+
46+
class Meta:
47+
managed = False
48+
db_table = "movies"
49+
50+
def __str__(self):
51+
return self.title
52+
53+
54+
class Users(models.Model):
55+
name = models.CharField(max_length=100)
56+
email = models.CharField(max_length=200)
57+
password = models.CharField(max_length=100)
58+
59+
class Meta:
60+
managed = False
61+
db_table = "users"
62+
63+
def __str__(self):
64+
return self.name
65+
66+
The ``Movies`` model represents the ``sample_mflix.movies`` collection
67+
and stores information about movies. The ``Users`` model represents
68+
the ``sample_mflix.users`` collection and stores account credentials
69+
for users of a movie streaming platform.
70+
71+
.. step:: Create views to display data
3272

3373
Open the ``views.py`` file in your ``sample_mflix`` directory and replace
3474
its contents with the following code:
3575

3676
.. code-block:: python
3777

3878
from django.http import HttpResponse
79+
from django.shortcuts import render
3980

81+
from .models import Movies, Users
4082

4183
def index(request):
42-
return HttpResponse("Hello, world. You're at the application index.")
84+
return HttpResponse("Hello! You've reached the Django MongoDB sample app landing page.")
4385

44-
.. step:: Configure a URL
86+
def recent_movies(request):
87+
movies = Movies.objects.all().order_by('-released')[:3]
88+
return render(request, 'recent_movies.html', {'movies': movies})
4589

46-
Create a new file called ``urls.py`` file in your ``sample_mflix`` directory and add
47-
the following code:
90+
def users_list(request):
91+
users = Users.objects.all().order_by('name')[:10]
92+
return render(request, 'users_list.html', {'users': users})
93+
94+
These views display a landing page message and information about your ``Movies``
95+
and ``Users`` models.
96+
97+
.. step:: Configure URLs for your views
98+
99+
Create a new file called ``urls.py`` file in your ``sample_mflix`` directory.
100+
To map the views defined in the preceding steps to URLs, paste the following
101+
code into ``urls.py``:
48102

49103
.. code-block:: python
50104

@@ -54,6 +108,8 @@ Create an Application
54108

55109
urlpatterns = [
56110
path("", views.index, name="index"),
111+
path('recent_movies/', views.recent_movies, name='recent_movies'),
112+
path('users_list/', views.users_list, name='users_list'),
57113
]
58114

59115
Then, navigate to the ``quickstart/urls.py`` file and replace its contents with
@@ -69,38 +125,77 @@ Create an Application
69125
path("admin/", admin.site.urls),
70126
]
71127

72-
Visit http://127.0.0.1:8000/sample_mflix/ to see the text defined in your view.
128+
.. step:: Create a template to format your views
73129

74-
.. step:: Apply database migrations
75-
76-
From your project root, run the following command:
130+
In your ``sample_mflix`` directory, create a subdirectory called
131+
``templates``. Then, create a file called ``recent_movies.html``
132+
and paste the following code:
77133

78-
.. code-block:: bash
79-
80-
python3 manage.py migrate
81-
82-
.. step:: Create models for movie and user data
83-
84-
Open the ``models.py`` file in the ``sample_mflix`` directory and replace
85-
its contents with the following code:
86-
87134
.. code-block:: python
88135

89-
import datetime
90-
91-
from django.db import models
136+
<!-- templates/recent_movies.html -->
137+
<!DOCTYPE html>
138+
<html lang="en">
139+
<head>
140+
<meta charset="UTF-8">
141+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
142+
<title>Recent Movies</title>
143+
</head>
144+
<body>
145+
<h1>Three Most Recent Movies</h1>
146+
<ul>
147+
{% for movie in movies %}
148+
<li>
149+
<strong>{{ movie.title }}</strong> (Released: {{ movie.released }})
150+
</li>
151+
{% empty %}
152+
<li>No movies found.</li>
153+
{% endfor %}
154+
</ul>
155+
</body>
156+
</html>
157+
158+
This template formats the movie data requested by the ``recent_movies`` view.
92159

160+
Create another file in the ``sample_mflix/templates`` directory called
161+
``users_list.html`` and paste the following code:
93162

94-
class Movies(models.Model):
95-
title = models.CharField(max_length=200)
96-
plot = models.CharField(max_length=800)
97-
runtime = models.IntegerField(default=0)
98-
released = models.DateTimeField("release date", null=True)
163+
.. code-block:: python
99164

100-
class Users(models.Model):
101-
name = models.CharField(max_length=100)
102-
email = models.CharField(max_length=200)
103-
password = models.CharField(max_length=100)
165+
<!-- templates/users_list.html -->
166+
<!DOCTYPE html>
167+
<html lang="en">
168+
<head>
169+
<meta charset="UTF-8">
170+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
171+
<title>Users List</title>
172+
</head>
173+
<body>
174+
<h1>Users List</h1>
175+
<table>
176+
<thead>
177+
<tr>
178+
<th>Name</th>
179+
<th>Email</th>
180+
</tr>
181+
</thead>
182+
<tbody>
183+
{% for user in users %}
184+
<tr>
185+
<td>{{ user.name }}</td>
186+
<td>{{ user.email }}</td>
187+
</tr>
188+
{% empty %}
189+
<tr>
190+
<td colspan="2">No users found.</td>
191+
</tr>
192+
{% endfor %}
193+
</tbody>
194+
</table>
195+
</body>
196+
</html>
197+
198+
This template formats the user data requested by the ``users_list`` view.
104199

105200
.. step:: Include your app in your project
106201

@@ -129,3 +224,6 @@ Create an Application
129224

130225
python3 manage.py makemigrations sample_mflix
131226
python3 manage.py migrate
227+
228+
After completing these steps, you have a basic {+django-odm+} app that
229+
you can use to interact with the ``sample_mflix`` database.

source/django-get-started/django-write-data.txt

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,64 @@ Write Data to MongoDB
2323

2424
python3 manage.py shell
2525

26-
.. step:: Insert a movie into the database
26+
.. step:: Import the required classes and modules
2727

28-
From your Python shell, run the following commands:
28+
From your Python shell, run the following code to import
29+
your models and the modules for creating a ``datetime`` object:
2930

3031
.. code-block:: python
3132

3233
from sample_mflix.models import Movies, Users
33-
parasite = Movies(title="Parasite", runtime=132)
34-
parasite.save()
34+
from django.utils import timezone
35+
from datetime import datetime
36+
37+
.. step:: Insert a movie into the database
38+
39+
Run the following code to create a ``Movies`` model that
40+
stores data about a movie titled ``"Minari"``:
41+
42+
.. code-block:: python
43+
44+
movie = Movies(title="Minari", plot=
45+
"A Korean-American family moves to an Arkansas farm in search of their own American Dream",
46+
runtime=117, released=timezone.make_aware(datetime(2020, 1, 26, 0, 0),
47+
timezone.get_current_timezone()))
48+
49+
Save the model to store its data as a document in the ``movies``
50+
collection, as shown in the following code:
51+
52+
.. code-block:: python
53+
54+
movie.save()
55+
56+
.. step:: Insert a user into the database
57+
58+
Run the following code to create a ``Users`` model that
59+
stores data about a user named ``"Abigail Carter"``:
60+
61+
.. code-block:: python
62+
63+
user = Users(name="Abigail Carter", email="[email protected]",
64+
password="secure123")
65+
66+
Save the model to store its data as a document in the ``users``
67+
collection, as shown in the following code:
68+
69+
.. code-block:: python
70+
71+
user.save()
72+
73+
.. step:: Render your new models
74+
75+
To ensure that your ``Movies`` model was inserted into the database,
76+
visit the http://127.0.0.1:8000/sample_mflix/recent_movies/ URL.
77+
You should see a list of five movies in the ``sample_mflix.movies``
78+
database, with your new movie listed at the top.
79+
80+
Then, ensure that your ``Users`` model was inserted into the
81+
database by visiting the http://127.0.0.1:8000/sample_mflix/users_list/
82+
URL. You should see a list of ten users in the ``sample_mflix.users``
83+
database, with your new user listed at the top.
3584

36-
This code inserts a movie with a ``title`` value of ``"Parasite"`` into
37-
the ``sample_mflix`` MongoDB database.
85+
After completing these steps, you have new documents in the ``movies``
86+
and ``users`` collection in the ``sample_mflix`` sample database.

0 commit comments

Comments
 (0)