In this section, we will guide you through the process of creating your first Django project. By the end of this lesson, you will have a basic Django project up and running.
Prerequisites
Before you start, ensure you have:
- Python installed on your machine.
- Django installed (if not, refer to the previous section on setting up the development environment).
Steps to Create Your First Django Project
- Create a Project Directory
First, create a directory where your Django project will reside. Open your terminal or command prompt and run:
- Create a Virtual Environment
It's a good practice to create a virtual environment for your project to manage dependencies. Run the following commands:
- Install Django
With the virtual environment activated, install Django using pip:
- Create a Django Project
Use the django-admin command to create a new Django project. Replace myproject with your desired project name:
This command creates a new directory named myproject with the following structure:
- Understand the Project Structure
Here's a brief overview of the files and directories created:
manage.py: A command-line utility that lets you interact with your Django project.myproject/: The inner directory containing the project settings and configurations.__init__.py: An empty file that tells Python this directory should be considered a package.settings.py: Configuration settings for the project.urls.py: URL declarations for the project.wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
- Run the Development Server
Navigate to the outer myproject directory and run the development server:
You should see output similar to this:
Open your web browser and go to http://127.0.0.1:8000/. You should see the Django welcome page, indicating that your project is set up correctly.
- Create a Django App
Django projects are composed of multiple apps. To create an app, run the following command inside your project directory:
This creates a new directory named myapp with the following structure:
- Register the App
To make Django aware of your new app, you need to add it to the INSTALLED_APPS list in settings.py:
- Create a Simple View
Let's create a simple view in myapp/views.py:
# myapp/views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, world! This is my first Django app.")
- Map the View to a URL
Create a urls.py file in the myapp directory and map the view to a URL:
# myapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]Include this URL configuration in the project's urls.py:
# myproject/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
- Test the View
Run the development server again:
Navigate to http://127.0.0.1:8000/ in your web browser. You should see the message "Hello, world! This is my first Django app."
Summary
In this section, you learned how to:
- Set up a Django project.
- Understand the basic project structure.
- Create and register a Django app.
- Create a simple view and map it to a URL.
You now have a basic Django project up and running. In the next section, we will dive deeper into Django's project structure and explore its components in more detail.
Django Web Development Course
Module 1: Introduction to Django
- What is Django?
- Setting Up the Development Environment
- Creating Your First Django Project
- Understanding Django Project Structure
Module 2: Django Basics
- Django Apps and Project Structure
- URL Routing and Views
- Templates and Static Files
- Models and Databases
- Django Admin Interface
Module 3: Intermediate Django
Module 4: Advanced Django
- Advanced Querying with Django ORM
- Custom User Models
- Django Signals
- Testing in Django
- Performance Optimization
