Introduction:
Embarking on the journey of creating your Django project is an exhilarating experience. However, it requires a series of crucial steps to ensure a smooth development process. This guide will walk you through 13 simple steps to create and set up your initial Django project and application. By the end of this tutorial, you will have a strong foundation to kickstart your Django web development journey.
Step 1: Installation and virtual env creation
Install Python: Django is a Python web framework, so having Python installed on your system is essential. You can download Python from the official website (https://www.python.org/downloads/) and follow the installation instructions.
Create a Virtual Environment
# Create a virtual environment named 'myworld'
python -m venv myworld
# Activate the virtual environment (Windows)
myworld\Scripts\activate.bat
Install Django: Django can be easily installed using Python's package manager, pip. Open your command prompt or terminal and run
the following command:
pip install django
Now, let's begin setting up your Django project!
Step 2: Create a New Django Project
Open your preferred code editor (e.g., Visual Studio Code, PyCharm) and navigate to the directory where you want to create your Django project. In your code editor's terminal, run the following command to create a new Django project (we'll use 'Home' as the project name):
django-admin startproject Home
This command generates the initial folder structure for your project.
Step 3: Enter the Project Directory
Navigate into your newly created project folder using the following command:
cd Home
Now you're inside your Django project directory, ready to configure your application.
Step 4: Create Your First Django App
Django projects consist of one or more apps that serve specific functionalities. To create your first app (in this case, named 'app'), run the following command within your project directory:
python manage.py startapp app
Step 5: Register Your App
To make your Django project aware of the new app, you need to register it. Open the 'settings.py' file located in your project folder and add 'app' to the INSTALLED_APPS list.
INSTALLED_APPS = [
'app',
# ...
]
Step 6: Create Static and Templates Folders
Create two essential folders in your project directory:
Static Folder: This folder will contain static files like images, stylesheets, and JavaScript files that will be accessible to visitors.
Templates Folder: This folder will hold dynamic files, such as HTML templates, that are not directly accessible to visitors.
You can create these folders manually in your project directory.
Step 7: Set Static Directories
In the 'settings.py' file, configure the static directory by adding the following code:
import os
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
This code ensures that your static files are correctly served.
Step 8: Set Template Directories
Still in the 'settings.py' file, configure the template directory by adding the following code:
TEMPLATES = [
{
# ...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
# ...
},
]
This code tells Django where to find your HTML templates.
Step 9: Create Templates
Templates are the building blocks of your web pages. Create HTML templates in the 'templates' folder to structure your website. You can use {% extends 'base.html' %}
to extend templates and include styles and scripts from the static folder.
Step 10: Set URLs for Your Project and App
Configure the URLs for your project and app. In your project's 'urls.py' file, import 'include' from 'django.urls' and set up the URL patterns for your app
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls')), # Include your app's URLs
]
Step 11: Define and Render URLs
Define the URL routes for your app in the 'urls.py' file of your 'app' app. Then, render these URLs in your 'views.py' file.
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {})
Step 12: Create a Super-User
Django provides a powerful admin panel that allows you to manage your application's data. To access it, create a superuser account by running this command:
python manage.py createsuperuser
Step 13: Run Migrations
Run database migrations to apply changes to your database schema
python manage.py makemigrations
python manage.py migrate
Step 14: Run Your Project
You've completed all the essential steps. Now, it's time to run your Django project using the following command:
python manage.py runserver
Visit 'http://127.0.0.1:8000/'
in your web browser to see your first Django web application in action. To access the admin panel, go to 'http://127.0.0.1:8000/admin/'
.
Final Thoughts:
Building your first Django project is an exciting journey. While this guide covers the fundamental steps, there is much more to explore in Django. If you encounter unfamiliar terms or concepts, don't hesitate to explore them online or refer to the Django documentation, which is an invaluable resource for Django developers.
Thank you for following this comprehensive guide. If you have any questions or suggestions, please feel free to leave them in the comments section. We'll be happy to assist you on your Django development journey.Happy coding!
Top comments (0)