In this lesson, you will learn how to install Django and set up your development environment. A well-configured environment is crucial for developing Django applications efficiently.
Before proceeding, ensure you have the following installed: - Python 3.6 or higher - pip (Python package installer) - A code editor (e.g., Visual Studio Code, PyCharm, etc.)
To install Django, you can use the pip command. It's a good practice to use a virtual environment to manage dependencies for your projects. Here’s how to set it up:
Open your terminal or command prompt and navigate to your project directory. Then, run the following command to create a virtual environment:
python -m venv myenv
Replace myenv with your preferred environment name.
To activate the virtual environment, use the following command:
- On Windows:
bash
myenv\Scripts\activate
- On macOS/Linux:
bash
source myenv/bin/activate
Once the virtual environment is activated, install Django using pip:
pip install django
To verify that Django has been installed correctly, run:
python -m django --version
This command should output the version of Django that was installed.
After installing Django, you can create a new project. Run the following command to create a project named myproject:
django-admin startproject myproject
This command creates a directory structure for your project. Navigate into the project folder:
cd myproject
Your project structure will look like this:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
To start the development server, run:
python manage.py runserver
You should see output indicating the server is running. Open your web browser and navigate to http://127.0.0.1:8000/. You should see the Django welcome page.
Note: Forgetting to activate your virtual environment before installing Django will lead to installation in the global Python environment, which can cause conflicts.
Note: Ensure you are using the correct version of Python that is compatible with Django.
django-admin startproject.python manage.py runserver to see your project in action.myfirstproject and explore the project structure.http://127.0.0.1:8000/ in your web browser. What do you see?django-admin startproject to create a new project.