Django / Django Basics
Getting Started with Django
This tutorial will guide you through the basics of setting up a Django project and creating a simple web application. You'll learn how to install Django, start a new project, and …
Section overview
5 resourcesCovers the fundamental concepts of Django, including project structure, models, views, and templates.
Introduction
In this tutorial, we will learn how to get started with Django, a high-level Python web framework. Our goal is to set up a Django project and create a simple web application.
By the end of this tutorial, you should be able to:
- Install Django.
- Start a new Django project.
- Create and view a basic web page.
Prerequisites:
- Basic knowledge of Python
- Python installed on your machine
Step-by-Step Guide
- Installation: You can install Django via pip, Python’s package installer. Open your terminal and type:
pip install Django
- Starting a new project: After installation, we can create a new Django project using the command below:
django-admin startproject myproject
This will create a new Django project named "myproject."
- Starting the server: Navigate into your new project directory and start the server:
cd myproject
python manage.py runserver
You should see a message saying that the server is running and you can view the website at http://localhost:8000.
- Creating a new app: In Django, an app is a module within a project. Let's create an app named 'myapp':
python manage.py startapp myapp
- Creating a view: A view in Django is a Python function that receives a web request and returns a web response. In
myapp/views.py, add this:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
This simple view returns "Hello, Django!" when it gets a web request.
Code Examples
Let's look at some code examples:
- Mapping the view to a URL: Django uses URLconfs to decide which code to run for each URL. In
myapp/urls.py, add this:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
This code maps the URL of the homepage ('') to the home view.
- Including the app's URLconf in the project's URLconf: In
myproject/urls.py, add this:
from django.urls import include, path
urlpatterns = [
path('', include('myapp.urls')),
]
This code includes the URLs of 'myapp' in the project.
Summary
In this tutorial, we've learned how to install Django, start a new project, start the server, create an app, and create a view. The next step is to learn about Django's database models.
Resources:
Practice Exercises
- Exercise 1: Create a new Django project named 'exercise1' and start the server.
-
Solution: Follow the steps in the 'Starting a new project' and 'Starting the server' sections.
-
Exercise 2: In the 'exercise1' project, create a new app named 'myexerciseapp' and create a view that returns "Hello, Exercise!".
-
Solution: Follow the steps in the 'Creating a new app' and 'Creating a view' sections, but return "Hello, Exercise!" instead of "Hello, Django!".
-
Exercise 3: Map the view from Exercise 2 to the URL of the homepage.
- Solution: Follow the steps in the 'Mapping the view to a URL' and 'Including the app's URLconf in the project's URLconf' sections.
Tips for further practice:
- Try changing the message in the view.
- Try mapping the view to a different URL.
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.
Latest 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