Beginners Guide to Setup Django Project

Python
Django Project Setup

Getting Started – Django Project

Prerequisites

  • Python installed on our system.
  • Create directory for project and ‘cd’ into project directory.
  • Create a virtual environment for django project. (Click Here for virtual environment setup tutorial.)
  • Activate the virtual environment.

Installation

After activating the virtual environment install django by running pip command.

Create Basic Django Project

Once the django installed in the system. Create the django project by running the below command.

django-admin startproject core .

After creating project your project structure looks like below.

Django Project Structure

Project Structure

  • Test-Project: The root folder of our project. Which contains all our project files and virtual environment.
  • venv: The virtual environment folder for the project.
  • core: This folder contains the files which is created by django-admin command.
    • __init__.py: This is an empty file. Which tells python that this directory should be considered as a Python project.
    • asgi.py: This file is an entry-point for ASGI-compatible webservers to serve our project.
    • settings.py: This file is acts as a main settings file for our project. Which is used to configure the ‘templates’ directory, ‘static’ files directory and ‘media’ directory paths and ‘debut’ settings etc.
    • urls.py: This file acts as base url routing file for the all application we create under our project.
    • wsgi.py: This file is an entry-point for WSGI-compatible web servers to serve our project.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. Like starting server, making migrations for database, creating sure user etc,.

How To Start A Server

You can start a django server using the following command.

python manage.py runserver

The default server is starting on 127.0.0.1 port 8000. We can customize these by-passing arguments to runserver command.

python manage.py runserver 127.0.0.1:8080

How To Make Migrations & Migrate Changes to Database

python manage.py makemigration

By using this command, we can make migrations for the models.py file. models.py file contains the database structure.

To apply changes to the database by applying below command.

Conclusion

In this blog post we learned how to setup a basic django project. By using this knowledge, we can extend this template to full-fledged web application in future blog posts. Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *