In web development, crafting outstanding applications demands a firm command of the available tools. This guide delves into Django, a Python web framework streamlining app creation. Before starting, it's vital to distinguish between a Django app and a Django project. Both are pivotal in Django's architecture, serving separate roles. Let's learn how to set up a Django app and comprehend its distinction from a Django project.
To understand Django's structure, it's crucial to differentiate between a project and an app:
In simple terms, a project is like a bookshelf, while an app is like one of the books on that shelf.
To learn how to set up a Django project, refer to this Answer.
Each Django project can contain multiple Django applications. These applications are where most of the functionality of the project lies. Once the project is set up, it's time to create an app:
Navigate to the project directory by using the following command:
cd projectname
Create a new Django app with the following command:
python manage.py startapp appname
Remember to replace appname
with the preferred name for the app.
The generated app directory will have several files and a sub-directory:
models.py
: Defines the data models (database tables).
views.py
: Handles the logic and rendering of responses.
urls.py
: Contains URL patterns (not created by default, but can be added).
apps.py
: Contains the app's configuration.
admin.py
: Allows customization of the app's representation in the Django admin site.
migrations/
: Stores migration files to keep track of changes to models.
The new application has to be registered with the project. Open the file named 'settings.py' located inside the project directory. In this file, there's a list called INSTALLED_APPS
. Add the name of the application to this list.
The updated INSTALLED_APPS
will look like this:
INSTALLED_APPS = [#....#append the following line'appname', # the app we created]
Remember to replace appname
with the name of the application.
The next step is building a view in the Django app. The purpose of a view is to render a template or to redirect to another view. Open the file 'views.py' located within the app directory and insert the following code:
from django.http import HttpResponsefrom django.shortcuts import renderdef home(request):return HttpResponse("Welcome, This is Educative!")
In the above code, a simple view named 'home' is created which returns an HttpResponse
saying "Welcome, This is Educative!".
To make the view accessible through a browser, a URL needs to be associated with it. This can be done by creating a 'urls.py' file within the app directory and adding this code:
from django.urls import pathfrom . import viewsurlpatterns = [path('', views.home, name='home'),]
This file essentially maps the URL to the view 'home'
. The empty string indicates the root URL of this application.
Finally, add the app URLs to the project URLs. Open 'urls.py' inside the projectname
directory and add this code:
from django.contrib import adminfrom django.urls import include, pathurlpatterns = [path('admin/', admin.site.urls),path('', include('appname.urls')), # include the app URLs]
Replace appname
with the name of the app.
The Django app is now ready. Run the following command to start the server:
python manage.py runserver
Here is the executable Django application. Click the "Run" button to execute the application.
By following this guide, a Django app is set up within a Django project. As the app develops, models can be defined, views can be expanded, and more functionality can be added. Remember, the power of Django apps lies in their reusability. Design them modularly, and they can become valuable components for multiple projects.
Free Resources