Apps

What are Apps?

In Django, apps are self-contained pieces of functionality that contribute to the site and provide one key function. E.g. for a Facebook clone you might have an app for newsfeed, messaging, adding friends etc. They are a reusable, self-contained collection of code that can be passed around project to project to speed up development. They typically exist as folders in your root directory that are created when you start them.

Creating an App

To create an app, run the following in the terminal

python manage.py startapp app_name

This will create a folder in your route directory named app_name.

Go to settings.py and add your app name to the list of INSTALLED_APPS

For a project using a backend, go ahead and create a models.py file in the app folder.

For apps with templates associated with them (which will be most apps), go ahead and create a folder inside this app folder named 'templates' and another folder inside this with the same name as the app to hold your HTML files. An example route would be app_name/templates/app_name/index.html. This is so that when Django looks for template names, it won't use a template with the same name from another app. We generally want a new template for each part of CRUD so for example there may be multiple delete.html files across your project. To render templates in your app, see the section on templates and views .

Also create a urls.py file to handle the routing, linked to the urls.py file in the project_name folder. For example project_name/urls.py might have:

from django.urls import path, include

urlpatterns = [
    path('HTML/', include('HTML.urls')),
]

In your app named HTML would have a urls.py file with:

from django.urls import path, include

urlpatterns = [
    path('HTML/', include('HTML.urls')),
]

In your app named HTML would have a urls.py file with:
from django.urls import path
from . import views

urlpatterns = [
    path('', views.html_home, name="html _home"),
    path(
        'introducing_elements/',
        views.HTML_introducing_elements,
        name="HTML_introducing_elements"
    ),
]

In this way, the project_name folder urls.py will set up any HTML app urls with the prefix of HTML/. This gives the two urls in the HTML app urls.py file final urls of www.website_name.com/HTML/ and www.website_name.com/HTML/introducing_elements/ respectively.

More on that can be seen in the templates section and the urls section.

The Core App

It is common to see an app named "core" in projects. These handle site-wide functionality that can be passed to all templates through creating context processors. This is especially useful for passing information from the View (through the context processor) to Templates that otherwise do not have an associated view.py file such as error pages.

Create an app named core as usual:

python manage.py startapp core

And add "core" to the list of installed apps in the settings.py file.

In the core app directory, create a context_processor.py file and add things that will be passed to your templates. For example, in the one for this website I use it to add the address for the MEDIA_URL so that I can call it in the template without having to pass it through a views.py file. Be sure to register this context_processor to the context_processors listed in settings.py. These are found in the TEMPLATES variable and will be in the form of app_name.file_name.function_name. For the below example, adding the media_url function so my addition to the context_processors will look like 'core.context_processor.media_url'.

from django.conf import settings

def media_url(request):
    return {
        'MEDIA_URL': settings.MEDIA_URL
    }

Then the vairable can be called in any Template like:

<img src="{{ MEDIA_URL }}errors/404-image.png" alt="Not Found">