Django / Django Basics
Creating Your First Django App
This tutorial is designed to help beginners create their first web application using Django. We'll walk through the process of creating a new app, defining a model, creating a vie…
Section overview
5 resourcesCovers the fundamental concepts of Django, including project structure, models, views, and templates.
Introduction
In this tutorial, we aim to guide you through the process of creating your first Django web application. We'll create a new app, define a model, create a view, and set up a URL for your app.
By the end of this tutorial, you will be able to:
- Set up a new Django project and app
- Create a basic model in Django
- Understand Django's MVT (Model, View, Template) architecture
- Create a simple view and map it to a URL
Prerequisites:
- Basic knowledge of Python
- Django installed on your system
Step-by-Step Guide
Creating a new Django project
- Open your terminal and navigate to the directory where you want your project to live.
- Type
django-admin startproject myprojectto create a new Django project. Replace 'myproject' with the name you want for your project.
Creating a new Django app
- In your terminal, navigate to your project directory using
cd myproject. - Type
python manage.py startapp myappto create a new app within your project. Replace 'myapp' with the name you want for your app.
Defining a model
In Django, a model is a representation of a database table. Let's create a simple model:
- Navigate to
models.pyinside your app directory. - Define a new model as follows:
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
Creating a view
A view in Django is a Python function that receives a web request and returns a web response. Let's create a simple view:
- Navigate to
views.pyinside your app directory. - Define a new view as follows:
from django.http import HttpResponse
from .models import MyModel
def my_view(request):
my_model = MyModel.objects.first()
return HttpResponse(my_model.name)
Setting up a URL
- Create a new file called
urls.pyinside your app directory and define your URLs as follows:
from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name='my_view'),
]
- In your project's
urls.py, include the URLs of your app:
from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')),
]
Code Examples
Here are the code snippets for the step-by-step guide, explained in detail:
models.py
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100) # A character field with a maximum length of 100 characters
description = models.TextField() # A text field for longer inputs
views.py
from django.http import HttpResponse
from .models import MyModel
def my_view(request):
my_model = MyModel.objects.first() # Fetches the first entry in MyModel
return HttpResponse(my_model.name) # Returns the name attribute of the fetched model
urls.py (App-level)
from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name='my_view'), # Mapping the view to the root URL of the app
]
urls.py (Project-level)
from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')), # Including all URLs from the 'myapp' app
]
Summary
In this tutorial, we learned how to set up a new Django project and app, define a basic model, create a simple view, and map that view to a URL. You can now create simple web applications using Django!
Next steps:
- Learn about Django's template system and create your first template
- Understand how to handle forms in Django
- Learn about Django's admin interface
Additional resources:
- Django's official documentation
- Django for Beginners
Practice Exercises
- Create a new Django project and app. Define a model with three different fields (CharField, TextField, and IntegerField).
- Create a view that fetches all entries in your model and returns their names in a HttpResponse.
- Map your view to a URL of your choice.
Solutions will vary depending on the specifics you chose for your project, app, model, and URL. As a tip for further practice, try to add more complex fields to your models, such as DateField or ForeignKey.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article