Starting Your Django Adventure: Setting Up Your Environment and Project Creation

Create your First Django Project.

Starting Your Django Adventure: Setting Up Your Environment and Project Creation

Introduction

Django is a Python web framework that allows for the rapid development of secure and maintainable web applications. It adheres to the Model-View-Controller (MVC) architectural pattern and prioritizes code reusability. Django is a popular open-source framework because of its powerful features such as an automatic admin interface, robust security, and scalability.

Setting up an Environment and Install Django

Before we get started with Django, let's make sure we have all of the necessary tools installed on our system. Here's what you'll require:

  1. Python: Django is written in Python, so you must have Python installed on your system.

Check Installation

    $ python3 -V
  1. Pip: Pip is the package manager for Python. You can install it by running the following command in your terminal:

     $ python -m ensurepip --default-pip
    

    You can similarly check that pip3 is installed by listing the available packages:

     $ pip3 list
    
  2. Virtualenv: Virtualenv is a tool that creates isolated Python environments. It's recommended to use a virtual environment to avoid conflicts with other packages installed on your system. You can install virtualenv by running the following command in your terminal:

     $ pip install virtualenv
    

Once you have installed the necessary tools, let's create a new virtual environment and install Django.

  1. Create a new directory for your project:

     $ mkdir myproject
    
  2. Navigate to the new directory:

     $ cd myproject
    
  3. Create a new virtual environment:

     $ virtualenv env
    
  4. Activate the virtual environment:

     $ source env/bin/activate  # for Mac/Linux
     $ env\Scripts\activate  # for Windows
    

    Now, you see the activated virtual environment:

     (env) $
    
  5. Install Django:

     $ pip install Django
    

Create a new Django project

Now that we have set up our environment, let's create a new Django project.

  1. Create a new Django project:

     (env) $ django-admin startproject myproject
    
  2. This will create a new directory called "myproject" with the following structure:

  3. Navigate to the new project directory:

     (env) $ cd myproject
    
  4. Run the development server:

     (env) $ python3 manage.py runserver
    
     ## the above command will result this.
     Watching for file changes with StatReloader
     Performing system checks…
     System check identified no issues (0 silenced).
     You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.Run 'python manage.py migrate' to apply them.
     March 01, 2022 - 01:19:16
     Django version 4.0.2, using settings 'myproject.settings'Starting development server at http://127.0.0.1:8000/
     Quit the server with CONTROL-C.
    
  5. This will start the development server at http://127.0.0.1:8000/.

  6. Open a web browser and go to http://127.0.0.1:8000/. You should see the default Django welcome page.

Congratulations! You've successfully installed Django and launched your first Django project.

Conclusion

Django is a robust web framework that enables the rapid creation of online applications. You can set up your environment and begin developing your own Django projects by following the instructions indicated in this blog post. Have fun coding!