For creating your very first django project, you have to follow some steps. Here I’m discussing all the steps in detail:
Check Python: - First, open your Terminal / Command Prompt and Write the following command:
python -–version
If you see Python 3.11.1 or Python 3.0 and upper. Then, your machine is ready for installing Django but if you see "python: command not found" message then go to DOWNLOAD PYTHON and install Python latest version.
Virtual Machine: - For do not affect any other project or separate this project from other projects, you need a Python virtual machine. To create a Python virtual machine just write on your selected folder terminal :
py –m venv virtualMachineName
It creates a venv folder in your existing path folder.
Activate Virtual Machine:- To activate the virtual machine write on the terminal:
Powershell: ./venv/Scripts/activate
OR
Bash: source venv/Scripts/activate
And it activate your virtual environment.
Install Django: - After activating your virtual environment, install Django on this virtual environment just write terminal :
pip install Django
This will download and install the latest version of Django.
Install Django Project: - Once Django is installed, you can create a new project using the following command:
django-admin startproject projectname
Replace “projectname” with the desired name of your project and it will create a new director with the project structure.
Move To Project Directory: - To move into the project directory write on your terminal:
cd projectname
Migration: -Django uses migrations to manage database schema changes. Run the following commands to apply initial migrations:
python manage.py migrate
Create a Superuser: To access the Django admin interface, create a superuser by running:
python manage.py createsuperuser
Follow the prompts to set up your superuser account.
Start the Development Server:- To test your Django project locally, run the development server:
python manage.py runserver
Visit http://127.0.0.1:8000/
in your web browser to see the default Django welcome page.
Create a Django App:- Django projects are made up of apps. Create a new app within your project:
python manage.py startapp appname
Replace "appname" with the desired name of your app.
settings.py
file, add your newly created app to the INSTALLED_APPS
section.
Media and Static Settings:- For static and media file. Add the following code in the bottom of the settings file.
#Setting.py
# ===Static file Settings===
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'projectname/static')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media')
In urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
urlpatterns = urlpatterns + \
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + \
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
file of your app.Example:
#app: models.py
from django.db import models
# Create your models here.
class ClassName(models.Model):
# write according to your requirements
Make Migrations: - Run the following command to create migrations for your models:
python manage.py makemigrations
Apply Migrations: - Apply the migrations to update your database schema:
python manage.py migrate
views.py
file of your app and create corresponding templates.Example:
from django.shortcuts import render
# Write your View.
def viewsname(request):
# Write code according to your requirements
return render(request,'templatename.html')
urls.py
file of your app, and include them in the project's urls.py
. Example:
# urls.py
from django.urls import path,include
#In urlpatterns
path("pathName",include("appName.viewsName"),name="urlName"),
Run the Development Server Again: Start the development server and navigate to your app's URLs to see the changes:
python manage.py runserver
Your Django project is now set up and ready for development. If you are facing any difficulties or have any queries, feel free to comment. You can also visit Django Documentation for in-depth knowledge.
2 Comments
Tahidul Islam
Thanks, I installed it successfully
Ashrafur Rhaman
Keep it up, Bro??