
Step 1: Create a folder and install Virtual Environment
Create a folder in your computer( Ex: My Django App) and open that folder in Code Editor like VS code (Right click and choose "Open with VS Code").
py -m venv venv
Note: Here "venv" is a virtual environment name.
Step 2: Activate the Virtual Environment and install Django in a virtual environment
Activate the virtual environment using the following command
venv\Scripts\Activate.ps1
Note: If you cannot activate virtual environment and getting error message like "venv/Scripts/Activate.ps1 : File E:\Django for Production office apps\venv\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170."
Solution: You need to set powershell ExecutionPolicy to Unrestricted using the following command.Just paste the command in Power shell.
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Then install the Django using the following command
pip install django
Step 3: Create a main project and create a separate app
Create a main Django project using the following command
django-admin startproject main .
Note: Here "main" is the main project name. You can use the command without the dot "django-admin startproject main". Using the command without dot will create a folder with your main project name and add files with the same name inside the folder.
If you want to create seperate app, use this command:
python manage.py startapp myapp
Note: Here "myapp" is an app name.
Step 4: Connect the app with the main project and connect the separate app URLs to the main project
Open main app settings.py file: main/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'myapp', # <-- add this line
]
If your app doesn’t have a urls.py, create one:
# myapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Then, connect your app’s URLs to your main project’s urls.py:
# project_name/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')), # connect the app
]
Step 5: Run the app.
Run the app using the following command
py manage.py runserver
Step 6 (optional): Create another separate app to add a Custom user model (if it's necessary)
© 2024 Webapptiv. All rights reserved.