-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOCSP-43200: Get started with Django #132
Closed
norareidy
wants to merge
34
commits into
mongodb:django-mongodb-backend
from
norareidy:DOCSP-43200-django-get-started
Closed
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
40ffb76
DOCSP-43200: Get started with Django
norareidy be878e5
edits
norareidy 704c54e
edit code
norareidy 1caf1a3
more info
norareidy 4142196
query steps
norareidy aa27f12
edit
norareidy 8585730
edits
norareidy e520bf4
landing page
norareidy 123a2d5
wording
norareidy d439dac
fix query structure
norareidy 7bb56ea
fixes
norareidy 2d3224f
template edit
norareidy 0fbb2e1
copyable code blocks
norareidy 735bccf
connection string
norareidy 4d9999e
fix image
norareidy a14aae2
AR feedback
norareidy b353a24
fix
norareidy 711af57
fix admonition
norareidy f2b7d40
wording
norareidy 6b599a7
admin site, more crud operations
norareidy aa3814b
edits
norareidy a993adf
more wording changes
norareidy c0c53b1
fix intro text
norareidy 6bd451d
fix
norareidy 6f46a1c
django_mongodb_backend
norareidy be3b2ed
AC feedback
norareidy 595ee65
next steps edits
norareidy c863134
TG feedback
norareidy ecff738
spacing
norareidy 1c06aec
parse_uri edits
norareidy 58b3541
TG feedback 2
norareidy 7bf9018
connection string
norareidy 7a54a24
code fix
norareidy ea02256
model edit
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.. _django-get-started: | ||
|
||
=============================== | ||
Get Started with {+django-odm+} | ||
=============================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: tutorial | ||
|
||
.. meta:: | ||
:description: Learn how to create an app to connect to a MongoDB deployment by using Django MongoDB Backend. | ||
:keywords: quick start, tutorial, basics | ||
|
||
.. toctree:: | ||
|
||
Download & Install </django-get-started/django-install/> | ||
Create a Deployment </django-get-started/django-create-deployment/> | ||
Create a Connection String </django-get-started/django-connection-string/> | ||
Configure a MongoDB Connection </django-get-started/django-connect-mongodb/> | ||
Create an Application </django-get-started/django-create-app/> | ||
Write Data to MongoDB </django-get-started/django-write-data/> | ||
Query MongoDB Data </django-get-started/django-query-data/> | ||
Create an Admin Site </django-get-started/django-create-admin/> | ||
Next Steps </django-get-started/django-next-steps/> | ||
|
||
{+django-odm+} is a Django database backend that uses PyMongo to connect | ||
to MongoDB. This tutorial shows you how to create a Django app, connect to | ||
a MongoDB cluster hosted on MongoDB Atlas, and interact with data in your cluster. | ||
|
||
.. tip:: | ||
|
||
MongoDB Atlas is a fully managed cloud database service that hosts your MongoDB | ||
deployments. You can create your own free (no credit card required) MongoDB Atlas | ||
deployment by following the steps in this guide. | ||
|
||
Follow this tutorial to connect a sample Django application to a MongoDB Atlas | ||
deployment. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
.. _django-get-started-connect: | ||
|
||
================================= | ||
Configure your MongoDB Connection | ||
================================= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: tutorial | ||
|
||
.. meta:: | ||
:keywords: app, odm, code example | ||
|
||
After installing {+django-odm+} and creating a MongoDB Atlas deployment, | ||
you can create a Django project that connects to MongoDB. | ||
|
||
.. procedure:: | ||
:style: connected | ||
|
||
.. step:: Create a Django project | ||
|
||
From your shell, run the following command to create a | ||
new Django project called ``quickstart`` based on a custom template: | ||
|
||
.. code-block:: bash | ||
|
||
django-admin startproject quickstart --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/{+django-version-number+}.x.zip | ||
|
||
.. note:: Project Template | ||
|
||
The ``django-mongodb-project`` template resembles the default Django project | ||
template but makes the following changes: | ||
|
||
- Includes MongoDB-specific migrations | ||
- Modifies the ``settings.py`` file to instruct Django | ||
to use an ``ObjectId`` value as each model's primary key | ||
|
||
After running this command, your ``quickstart`` project has | ||
the following file structure: | ||
|
||
.. code-block:: bash | ||
:copyable: false | ||
|
||
quickstart/ | ||
manage.py | ||
mongo_migrations/ | ||
__init__.py | ||
contenttypes/ | ||
auth/ | ||
admin/ | ||
quickstart/ | ||
__init__.py | ||
apps.py | ||
settings.py | ||
urls.py | ||
asgi.py | ||
wsgi.py | ||
|
||
.. step:: Update your database settings | ||
|
||
Open your ``settings.py`` file and navigate to the ``DATABASES`` setting. | ||
Replace this setting with the following code: | ||
|
||
.. code-block:: python | ||
|
||
DATABASES = { | ||
"default": django_mongodb_backend.parse_uri("<connection string URI>"), | ||
} | ||
|
||
Replace the ``<connection string URI>`` placeholder with the connection string | ||
that you copied from the :ref:`django-get-started-connection-string` | ||
step of this guide. This configures your Django app to connect to | ||
your Atlas deployment and access the ``sample_mflix`` sample database. | ||
norareidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. step:: Start the server | ||
|
||
To verify that you installed {+django-odm+} and correctly configured | ||
your project, run the following command from your project root: | ||
|
||
.. code-block:: bash | ||
|
||
python manage.py runserver | ||
|
||
Then, visit http://127.0.0.1:8000/. This page displays a "Congratulations!" | ||
message and an image of a rocket. | ||
|
||
After completing these steps, you have a Django project configured | ||
to use MongoDB. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
.. _django-get-started-connection-string: | ||
|
||
========================== | ||
Create a Connection String | ||
========================== | ||
|
||
You can connect to your MongoDB deployment by providing a | ||
**connection URI**, also called a *connection string*, which | ||
instructs the driver on how to connect to a MongoDB deployment | ||
and how to behave while connected. | ||
|
||
The connection string includes the hostname or IP address and | ||
norareidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
port of your deployment, the authentication mechanism, user credentials | ||
when applicable, and connection options. | ||
|
||
To connect to an instance or deployment not hosted on Atlas, see | ||
:ref:`pymongo-connection-targets` in the PyMongo documentation. | ||
|
||
.. procedure:: | ||
:style: connected | ||
|
||
.. step:: Find your MongoDB Atlas connection string | ||
|
||
To retrieve your connection string for the deployment that | ||
you created in the :ref:`previous step <django-get-started-create-deployment>`, | ||
log into your Atlas account and navigate to the | ||
:guilabel:`Clusters` section and click the :guilabel:`Connect` button | ||
for your new deployment. | ||
|
||
.. figure:: /includes/figures/atlas_connection_connect_cluster.png | ||
:alt: The connect button in the clusters section of the Atlas UI | ||
|
||
Proceed to the :guilabel:`Connect your application` section and select | ||
"Python" from the :guilabel:`Driver` selection menu and the version | ||
that best matches the version you installed from the :guilabel:`Version` | ||
selection menu. | ||
|
||
.. step:: Copy your connection string | ||
|
||
Click the button on the right of the connection string to copy it to | ||
your clipboard as shown in the following screenshot: | ||
|
||
.. figure:: /includes/figures/atlas_connection_copy_string_python.png | ||
:alt: The connection string copy button in the Atlas UI | ||
|
||
.. step:: Edit and save the connection string | ||
|
||
Paste your connection string into a file in your preferred text editor | ||
and save this file to a safe location for use in the next step. | ||
Your connection string resembles the following example: | ||
|
||
.. code-block:: none | ||
:copyable: false | ||
|
||
mongodb+srv://<username>:<password>@samplecluster.jkiff1s.mongodb.net/?retryWrites=true&w=majority&appName=SampleCluster | ||
|
||
|
||
Replace the ``<username>`` and ``<password>`` placeholders with | ||
your database user's username and password. | ||
|
||
Then, specify a connection to the ``sample_mflix`` database from the | ||
Atlas sample datasets by adding it after the hostname, as shown in | ||
the following example: | ||
|
||
.. code-block:: none | ||
:copyable: false | ||
|
||
mongodb+srv://<username>:<password>@samplecluster.jkiff1s.mongodb.net/sample_mflix?retryWrites=true&w=majority&appName=SampleCluster | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may want to delete the stray blank line(s) at the end of some documents. ;-) |
||
After completing these steps, you have a connection string that | ||
contains your database username, database password, and database name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
.. _django-get-started-create-admin: | ||
|
||
==================== | ||
Create an Admin Site | ||
==================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: tutorial | ||
|
||
.. meta:: | ||
:keywords: app, odm, code example | ||
|
||
You can create a Django admin site to edit your application's | ||
data from a web interface. To learn more about the Django admin | ||
site and its features, see `The Django admin site <https://docs.djangoproject.com/en/{+django-version-number+}/ref/contrib/admin/>`__ | ||
in the Django documentation. | ||
|
||
.. procedure:: | ||
:style: connected | ||
|
||
.. step:: Create an admin user | ||
|
||
Before creating an admin site, you must create a user | ||
who can log in to the site. | ||
|
||
From your project's root directory, run the following command to create | ||
an admin user: | ||
|
||
.. code-block:: bash | ||
|
||
python manage.py createsuperuser | ||
|
||
Then, your terminal prompts you for a username, email address, | ||
and password. For each prompt, enter the following information | ||
to create a user with the specified credentials and press "enter" | ||
after each entry: | ||
|
||
.. code-block:: bash | ||
|
||
Username: admin | ||
Email address: [email protected] | ||
Password: <admin-password> | ||
Password (again): <admin-password> | ||
|
||
Replace the ``<admin-password>`` placeholder with your user's password. | ||
|
||
.. step:: Enter the admin site | ||
|
||
Run the following code to start your server: | ||
|
||
.. code-block:: bash | ||
|
||
python manage.py runserver | ||
|
||
Once your server is running, visit the http://127.0.0.1:8000/admin/ | ||
URL to see the admin site. This site displays the following login | ||
screen: | ||
|
||
.. figure:: /includes/figures/django_admin_login.png | ||
:alt: The login screen on the Django admin page. | ||
|
||
Enter the username and password created in the previous step to log in to | ||
the site. | ||
|
||
.. step:: Access your "sample_mflix" app from the admin site | ||
|
||
After logging in to the admin site, you can see the following information: | ||
|
||
.. figure:: /includes/figures/django_admin_index.png | ||
:alt: The initial content displayed on the Django admin site. | ||
|
||
You can edit your project's authentication configuration by selecting | ||
the :guilabel:`Groups` or :guilabel:`Users` row in the | ||
:guilabel:`Authentication and Authorization` table. | ||
|
||
To edit the data in the ``users`` sample collection, represented by your | ||
``Viewer`` model, navigate to your project's ``sample_mflix/admin.py`` file | ||
and paste the following code: | ||
|
||
.. code-block:: python | ||
|
||
from django.contrib import admin | ||
|
||
from .models import Viewer | ||
|
||
admin.site.register(Viewer) | ||
|
||
Now, your admin site displays the following information: | ||
|
||
.. figure:: /includes/figures/django_admin_model.png | ||
:alt: The content displayed on the Django admin site after registering a model. | ||
|
||
.. step:: Select a Viewer object | ||
|
||
You can view the data stored in a ``Viewer`` object that | ||
has a ``name`` value of ``"Abigail Carter"``. You created | ||
this object in the :ref:`django-get-started-write` step | ||
of this tutorial. | ||
|
||
Click on the :guilabel:`Viewers` row of the :guilabel:`SAMPLE_MFLIX` | ||
table to see a list of viewers. The admin site displays the following | ||
list: | ||
|
||
.. figure:: /includes/figures/django_admin_viewers.png | ||
:alt: The list of viewers displayed on the admin site. | ||
|
||
Then, click on :guilabel:`Abigail Carter` at the top of the list. | ||
The site displays the :guilabel:`Name`, :guilabel:`Email`, and :guilabel:`Password` | ||
of the selected viewer: | ||
|
||
.. figure:: /includes/figures/django_admin_edit_viewer.png | ||
:alt: The information of your selected viewer. | ||
|
||
.. step:: Edit the data in a Viewer object | ||
|
||
To edit the ``email`` field of the viewer, select the | ||
box that contains the text ``"[email protected]"``. | ||
Delete this text and replace it with ``"[email protected]"``, | ||
as shown in the following image: | ||
|
||
.. figure:: /includes/figures/django_admin_new_email.png | ||
:alt: The updated email address of your viewer. | ||
|
||
Then, click the :guilabel:`SAVE` button below the viewer's | ||
information to save your changes. | ||
|
||
After completing these steps, you can access the Django | ||
admin site and use it to edit your ``Viewer`` objects. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be aware this is changing to "django-mongodb-backend".