Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
In this tutorial, we'll guide you through the process of creating your first Django project.
Prerequisites:
Step 1: Create a new Django project
1.1. Open the terminal/command prompt and navigate to the directory where you want to create your project.
1.2. Run the following command to create a new Django project called 'myproject':
django-admin startproject myproject
This command will generate a new directory named 'myproject' with the following structure:
myproject/ manage.py myproject/ __init__.py asgi.py settings.py urls.py wsgi.py
Step 2: Familiarize yourself with the project structure
manage.py
: A command-line utility that allows you to perform various management tasks for your project, such as running the development server, running tests, and creating database tables.myproject/__init__.py
: An empty file that tells Python that the directory should be treated as a package.myproject/asgi.py
: An entry point for ASGI (Asynchronous Server Gateway Interface) servers. This is used when you deploy your project using an ASGI server.myproject/settings.py
: Contains settings and configurations for your project, such as database settings, time zone, and static files location.myproject/urls.py
: Defines the URL patterns for your project. This is where you include the URL patterns from your apps.myproject/wsgi.py
: An entry point for WSGI (Web Server Gateway Interface) servers. This is used when you deploy your project using a WSGI server.Step 3: Test your new project
3.1. Run your Django development server:
cd myproject python manage.py runserver
3.2. Open your web browser and visit http://127.0.0.1:8000/
. You should see the Django welcome page, which indicates that your project has been set up correctly.
You've now created your first Django project. To start building your application, you can create one or more apps within your project and define views, models, templates, and other components as needed. To create your first app, you can follow the "Create The First Application in Django Project" tutorial.
Setting up a Django project step by step:
# Create a new Django project django-admin startproject myproject # Navigate to the project directory cd myproject
Configuring settings in a Django project:
settings.py
file, including database connection, middleware, and installed apps.# settings.py in your project DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } }
Managing static files in a Django project:
# settings.py in your project STATIC_URL = '/static/'
Setting up virtual environments for Django projects:
# Create a virtual environment python -m venv venv # Activate the virtual environment source venv/bin/activate # On Linux/Mac .\venv\Scripts\activate # On Windows
Defining URLs in a Django project:
# urls.py in your project from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]
Creating and applying migrations in Django project:
# Create initial migration python manage.py makemigrations # Apply migrations python manage.py migrate