Creating Simple Django Application

Creating Simple Django Application

1.Install Django

  1. To install Django, you can follow the below steps:

    1. Make sure you have Python installed on your system. You can download the latest version of Python from the official website: https://www.python.org/downloads/

    2. Open the command prompt/terminal and check the version of Python installed on your system by typing the following command:

python --version

Install Django using pip (Python Package Manager) by running the following command in the terminal:

pip install Django

This command will download and install the latest stable version of Django.

  1. Once the installation is complete, you can verify that Django is installed correctly by typing the following command in the terminal:
python -m django --version

This command will display the version number of the Django framework that has been installed

2. Creating Virtual Environment

To create a virtual environment for your Python project, you can follow these steps:

  1. Open a terminal/command prompt and navigate to the directory where you want to create your project.

  2. Once you are in the desired directory, create a new virtual environment by running the following command:

python -m venv myenv

Here, "myenv" is the name of the virtual environment. You can choose any name for your virtual environment.

  1. Once you run the command, a new directory named "myenv" (or whatever you named your environment) will be created in your current directory. This directory contains all the necessary files for your virtual environment.

  2. Activate the virtual environment by running the following command:

source myenv/bin/activate    # For Unix/Linux
myenv\Scripts\activate.bat  # For Windows

This will activate the virtual environment and you should see the name of your virtual environment in the command prompt/terminal.

Note: In Windows, you need to use the backslash () instead of the forward slash (/) to navigate between directories.

  1. Once the virtual environment is activated, you can install any required packages using pip, and they will be installed in the virtual environment, isolated from the global Python installation.

To deactivate the virtual environment, simply run the following command:

deactivate

3.How to create a sample project

To create a sample project in Django, you can follow these steps:

  1. Open a terminal/command prompt and navigate to the directory where you want to create your project.

  2. Once you are in the desired directory, create a new virtual environment by following the steps mentioned in the previous answer.

  3. Activate the virtual environment by running the appropriate command for your operating system.

  4. Install Django in your virtual environment by running the following command:

pip install Django

Once Django is installed, create a new project by running the following command:

django-admin startproject myproject
  1. Here, "myproject" is the name of your project. You can choose any name for your project.

  2. After running the above command, a new directory named "myproject" will be created in your current directory. This directory contains all the necessary files for your Django project.

  3. Navigate to the project directory by running the following command:

cd myproject

Run the following command to create a new app in your project:

python manage.py startapp myapp

Finally, run the development server by running the following command:

python manage.py runserver
  1. This will start the development server at http://127.0.0.1:8000/. You can open this URL in your web browser to see the default Django "Welcome" page.

Congratulations! You have successfully created a sample project in Django.