@@ -28,23 +28,77 @@ Create an Application
28
28
The ``django-mongodb-app`` template ensures that your ``app.py`` file
29
29
includes the line ``"default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'"``.
30
30
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
32
72
33
73
Open the ``views.py`` file in your ``sample_mflix`` directory and replace
34
74
its contents with the following code:
35
75
36
76
.. code-block:: python
37
77
38
78
from django.http import HttpResponse
79
+ from django.shortcuts import render
39
80
81
+ from .models import Movies, Users
40
82
41
83
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 .")
43
85
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})
45
89
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``:
48
102
49
103
.. code-block:: python
50
104
@@ -54,6 +108,8 @@ Create an Application
54
108
55
109
urlpatterns = [
56
110
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'),
57
113
]
58
114
59
115
Then, navigate to the ``quickstart/urls.py`` file and replace its contents with
@@ -69,38 +125,77 @@ Create an Application
69
125
path("admin/", admin.site.urls),
70
126
]
71
127
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
73
129
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 :
77
133
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
-
87
134
.. code-block:: python
88
135
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.
92
159
160
+ Create another file in the ``sample_mflix/templates`` directory called
161
+ ``users_list.html`` and paste the following code:
93
162
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
99
164
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.
104
199
105
200
.. step:: Include your app in your project
106
201
@@ -129,3 +224,6 @@ Create an Application
129
224
130
225
python3 manage.py makemigrations sample_mflix
131
226
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.
0 commit comments