Let’s walk through the steps to create a basic Django project. I’ll assume you have Python and Django installed on your system. If not, make sure to install them before proceeding.
Personal Software/Tools Recommendation
I personally use and recommend you for use the bellow tools while developing Django Projects:
- Git Bash Command Line Interface for Windows
- Visual Studio Code IDE for writing code
- Django LTS Version
Create a Basic Django Project
Step 1: Create a Virtual Environment
It’s good practice to work within a virtual environment to isolate your project’s dependencies. Open your terminal (or command prompt) and navigate to the directory where you want to create the Django project. Then, create a virtual environment with the following command:
# On Windows python -m venv venv # On macOS/Linux python3 -m venv venv
Activate the virtual environment:
# On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
Step 2: Install Django
Once the virtual environment is activated, you can install Django using pip:
pip install django
Please note that we are using latest Django LTS Version: 4.2.3
Step 3: Create the Django Project
Create a new Django project using a placeholder name “myproject”:
django-admin startproject myproject .
Step 4: Create a Django App
Create a new Django app using a placeholder name “myapp”:
python manage.py startapp myapp
Step 5: Configure the Project Settings
Open the settings.py
file inside the myproject
folder and update the INSTALLED_APPS
list:
# myproject/settings.py INSTALLED_APPS = [ # ... 'myapp', ]
Step 6: Create a Simple View
In your app directory (myapp
), open the views.py
file and add a simple view function:
# myapp/views.py from django.http import HttpResponse def hello_world(request): return HttpResponse("Hello, world!")
Step 7: Define URL Patterns
Create a urls.py
file in your app directory (myapp
):
# myapp/urls.py from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello_world, name='hello_world'), ]
Step 8: Configure the Project URLs
Open the urls.py
file in the myproject
folder and update it as follows:
# myproject/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('myapp.urls')), ]
Step 9: Run the Development Server
Start the development server and test the simple view:
python manage.py runserver
Visit http://localhost:8000/hello/ in your web browser, and you should see the text “Hello, world!” displayed on the page.
Now, by using placeholder names like “myproject” and “myapp” in your project, app, and file names, you can easily reuse this template for multiple Django tutorials without the need to manually update those names. Simply replace “myproject” and “myapp” with the desired names for each, if needed. Happy coding!
Find this Project on Github