|
| 1 | +.. _django-get-started-create-app: |
| 2 | + |
| 3 | +===================== |
| 4 | +Create an Application |
| 5 | +===================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: app, odm, code example |
| 13 | + |
| 14 | +.. procedure:: |
| 15 | + :style: connected |
| 16 | + |
| 17 | + .. step:: Create a "sample_mflix" app |
| 18 | + |
| 19 | + From your ``quickstart`` project directory, run the following command to create a |
| 20 | + new Django app called ``sample_mflix`` based on a custom template: |
| 21 | + |
| 22 | + .. code-block:: bash |
| 23 | + |
| 24 | + python3 manage.py startapp sample_mflix --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.0.x.zip |
| 25 | + |
| 26 | + .. note:: App Template |
| 27 | + |
| 28 | + The ``django-mongodb-app`` template ensures that your ``app.py`` file |
| 29 | + includes the line ``"default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'"``. |
| 30 | + |
| 31 | + .. step:: Create a view |
| 32 | + |
| 33 | + Open the ``views.py`` file in your ``sample_mflix`` directory and replace |
| 34 | + its contents with the following code: |
| 35 | + |
| 36 | + .. code-block:: python |
| 37 | + |
| 38 | + from django.http import HttpResponse |
| 39 | + |
| 40 | + |
| 41 | + def index(request): |
| 42 | + return HttpResponse("Hello, world. You're at the application index.") |
| 43 | + |
| 44 | + .. step:: Configure a URL |
| 45 | + |
| 46 | + Create a new file called ``urls.py`` file in your ``sample_mflix`` directory and add |
| 47 | + the following code: |
| 48 | + |
| 49 | + .. code-block:: python |
| 50 | + |
| 51 | + from django.urls import path |
| 52 | + |
| 53 | + from . import views |
| 54 | + |
| 55 | + urlpatterns = [ |
| 56 | + path("", views.index, name="index"), |
| 57 | + ] |
| 58 | + |
| 59 | + Then, navigate to the ``quickstart/urls.py`` file and replace its contents with |
| 60 | + the following code: |
| 61 | + |
| 62 | + .. code-block:: python |
| 63 | + |
| 64 | + from django.contrib import admin |
| 65 | + from django.urls import include, path |
| 66 | + |
| 67 | + urlpatterns = [ |
| 68 | + path("sample_mflix/", include("sample_mflix.urls")), |
| 69 | + path("admin/", admin.site.urls), |
| 70 | + ] |
| 71 | + |
| 72 | + Visit http://127.0.0.1:8000/sample_mflix/ to see the text defined in your view. |
| 73 | + |
| 74 | + .. step:: Apply database migrations |
| 75 | + |
| 76 | + From your project root, run the following command: |
| 77 | + |
| 78 | + .. code-block:: bash |
| 79 | + |
| 80 | + python manage.py migrate |
| 81 | + |
| 82 | + .. step:: Create models for movie and users data |
| 83 | + |
| 84 | + Open the ``models.py`` file in the ``sample_mflix`` directory and replace |
| 85 | + its contents with the following code: |
| 86 | + |
| 87 | + .. code-block:: python |
| 88 | + |
| 89 | + from django.db import models |
| 90 | + |
| 91 | + |
| 92 | + class Movies(models.Model): |
| 93 | + name = models.CharField(max_length=200) |
| 94 | + runtime = models.IntegerField(default=0) |
| 95 | + |
| 96 | + |
| 97 | + class Users(models.Model): |
| 98 | + name = models.CharField(max_length=200) |
| 99 | + email = models.CharField(max_length=200) |
| 100 | + |
| 101 | + |
| 102 | + .. step:: Include your app in your project |
| 103 | + |
| 104 | + Open the ``settings.py`` file in ``quickstart`` and edit your |
| 105 | + ``INSTALLED_APPS`` setting to resemble the following code: |
| 106 | + |
| 107 | + .. code-block:: python |
| 108 | + |
| 109 | + INSTALLED_APPS = [ |
| 110 | + 'sample_mflix.apps.SampleMflixConfig', |
| 111 | + 'quickstart.apps.MongoAdminConfig', |
| 112 | + 'quickstart.apps.MongoAuthConfig', |
| 113 | + 'quickstart.apps.MongoContentTypesConfig', |
| 114 | + 'django.contrib.sessions', |
| 115 | + 'django.contrib.messages', |
| 116 | + 'django.contrib.staticfiles', |
| 117 | + ] |
| 118 | + |
| 119 | + .. step:: Create migrations for your new models |
| 120 | + |
| 121 | + From your project root, run the following command to create |
| 122 | + migrations for the ``Movies`` and ``Users`` models and Apply |
| 123 | + the changes to the database: |
| 124 | + |
| 125 | + .. code-block:: bash |
| 126 | + |
| 127 | + python3 manage.py makemigrations sample_mflix |
| 128 | + python3 manage.py migrate |
0 commit comments