Setting Up a Basic Django Project Using Pipenv in VS Code
After setting up your development environment with Python, Git, Pipenv, and Visual Studio Code, the next step is to create a Django project. This post will guide you through initializing a basic Django project managed by Pipenv inside VS Code.
After setting up your development environment with Python, Git, Pipenv, and Visual Studio Code, the next step is to create a Django project. This post will guide you through initializing a basic Django project managed by Pipenv inside VS Code.
1. Create a Project Folder
First, create a dedicated folder for your Django project. Open your command prompt or VS Code terminal and run:
`mkdir my_django_project
cd my_django_project
`
Replace my_django_project with your desired project name.
2. Initialize Pipenv Environment
Inside your project folder, initialize a new Pipenv environment with Python 3.12.4:
`pipenv --python 3.12.4
`
This creates a virtual environment isolated from your global Python installation.
3. Install Django
Next, install Django using Pipenv:
`pipenv install django
`
This command adds Django as a dependency in your Pipfile and installs it in your virtual environment.
4. Activate the Virtual Environment
To start using the virtual environment, activate it by running:
`pipenv shell
`
You should now see your shell prompt change, indicating you are inside the Pipenv-managed environment.
5. Create a Django Project
With the environment activated, create a new Django project by running:
`django-admin startproject mysite .
`
The dot . ensures the project files are created in the current directory.
6. Apply Database Migrations
Before running your project, apply the necessary database migrations:
`py manage.py makemigrations
py manage.py migrate
`
These commands create migration files and apply them to your database.
7. Create a Superuser
To access the Django admin interface, create a superuser account by running:
`py manage.py createsuperuser
`
Follow the prompts to set username, email, and password.
8. Run Django Development Server
Start the Django development server with:
`py manage.py runserver
`
Open your browser and go to http://127.0.0.1:8000/ to see the default Django welcome page.
9. Open the Project in VS Code
If you haven’t already, open the project folder in VS Code:
-
Launch VS Code
-
Click File > Open Folder...
-
Select your project folder (
my_django_project)
VS Code will recognize your Pipenv environment automatically if you have the Python extension installed.