Django Polls Simplified — Creating the Polls App
How to create a Django app, a continuation of:
Make you activate your virtual environment, where we already installed Django, and you are in django-polls working directory.
To create an app in Django, run the following(in the project directory, same as manage.py; make sure your environment is activated)
python manage.py startapp pollsA new directory call polls will be created in the root folder:
Follow along with the commit:
Registering Your App
After creating the polls app with python manage.py startapp polls, you’ll notice a new directory named polls in your project folder.
This directory contains the basic structure of your Django application. However, to fully integrate this new app into your Django project, you need to register it.
Registering your app in Django is essential because it tells Django that this app is part of the project and should be considered when performing operations like database migrations, running tests, or serving views.
Without registration, Django wouldn’t recognize your app's models, views, or other components.
To register the app, add 'polls.apps.PollsConfig' to the INSTALLED_APPS list in your project’s settings.py file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig', # Register your Polls App here
]This step ensures that Django can find the app’s configurations, like its models for migrations or its admin interface for the admin site.
Write your first view
Imagine you’re playing with building blocks and want to create a beautiful castle. In Django, views are like the special instructions you follow to build different parts of your castle.
Here’s how we can think about views:
- Views are like recipe cards: They tell Django how to make a specific part of your website, just like a recipe tells you how to make a yummy cake.
- Views decide what to show: When someone visits your website, views help decide what they should see, like choosing which toy to show your friend.
- Views can do magic tricks: They can take information from your computer and turn it into something fun to look at on the website, like turning a boring list into a colorful picture.
- Views are helpers: They work with other parts of Django to make your website work, just like you might work with your friends to build a big Lego tower.
So, when we write a view in Django, we’re telling our website, “Hey, when someone asks to see this part, here’s what you should show them and how it should look!”
Open the file polls/views.py and put the following Python code in it:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")This is the most basic view possible in Django. To access it in a browser, we need to map it to a URL — and for this, we need to define a URL configuration, or “URLconf” for short. These URL configurations are defined inside each Django app and are Python files named urls.py.
To define URL configurations for the polls app, create urls.py file inside the polls directory and add the following:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]Your app directory should now look like this:
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.pyThe next step is configuring the global URLconf in our django-polls project to include the URLconf defined in polls.urls.
To do this, add an import for django.urls.include in config/urls.py and insert an include() in the urlpatterns list, so you have:
"""
URL configuration for config project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include("polls.urls")),
path('admin/', admin.site.urls),
]The above setup means that any URL starting with / will look for its pattern in polls/urls.py. For instance, if polls/urls.py defines a URL pattern for ‘questions/’, accessing localhost:8000/questions/ would use the view associated with that URL in the polls app.
This modular approach to URL configuration in Django helps keep your project organized, especially when it grows large or contains multiple apps. Each app can manage its URLs, reducing complexity and enhancing maintainability.
Save the changes and run the server.
python manage.py runserverYou can follow along with the code up to this point by referring to the following commit:
NEXT ARTICLE:
