Skip to main content

First Django Project

Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc.

 

Setup

  1. Install Django version 3.0.1:
  2. sudo pip install django==3.0.1
  3. Check Django version:
  4. python -m django --version
  5. Create a new Django project:
  6. django-admin startproject project_name
  7. Navigate into the project directory:
  8. cd project_name
  9. Create a new app within the project:
  10. python manage.py startapp app_name
  11. Start the development server:
  12. python manage.py runserver
  13. To stop the server, use ctrl+z.

Database Migrations

  1. Create initial migrations:
  2. python manage.py makemigrations
  3. Apply migrations:
  4. python manage.py migrate

    This applies migrations for built-in models like admin, auth, contenttypes, and sessions.

Exploring Models

To see the default models available in your project:

  1. Open the Django shell:
  2. python manage.py shell
  3. Run the following commands:
  4. from django.apps import apps
    
    for app in apps.get_app_configs():
        models = app.get_models()
        for model in models:
            print(model.__name__)
        

Viewing Model Data

To see the data in a model:

  1. Open the Django shell:
  2. python manage.py shell
  3. Import the necessary model:
  4. from django.contrib.auth.models import User
  5. Retrieve and print user data:
  6. users = User.objects.all()
    for user in users:
        print(user.username)
        

Comments

Popular posts from this blog

LinkedList

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.