Django Commands


Here are some useful DJango commands for beginners.

Using pip:

To install packages

>>> pip install django

To uninstall packages

>>> pip uninstall django

Creating Virtual Environment

On Unix or Mac OS

>>> python3 -m venv name-env

On Windows

>>> python -m venv name-env


Activating Virtual Environment

On Windows

>>> source name-env\Scripts\activate

On Unix or Mac OS

>>> source name-env/bin/activate

Starting Django Project

>>> django-admin startproject <name>

Creating an app

>>> python manage.py startapp <name>

Different ModelFields

CharField(max_length=None)

DateTimeField(auto_now=False,auto_now_add=False)

EmailField(max_length=None)

FileField(upload_to=None,max_length=100)

ImageField(upload_to=None,max_length=100)

IntegerField()

SlugField(max_length=50)

TextField(max_length=1000)

URLField(max_length=100)

ForeignKey(ModelName,on_delete=models.CASCADE)


Making Migrations

>>> python manage.py makemigrations
>>> python manage.py migrate

Creating a SuperUser

>>> python manage.py createsuperuser


Syntax for accessing variables:

- Accessing Value {{ name }}
- Setting url navigation {% url ‘ ’ %}
- Loading static {% load static %}


Using Conditional:

{% if expression %}

{% elif expression %}

{% else %}

{% endif %}

Looping using for loop

{% for item in items %}

{% endfor %}

Including partials

{% include ‘path_to_partial’ %}


Doing Search Queries

from django.db.models import Q
Q(ModelField__startswith=’term’)

Q(ModelField__icontains=’term’)

Q(ModelField__iexact=’term’)

Q(ModelField__lt=20) # less than

Q(ModelField__lte=20) # less than or equal to

Q(ModelField__gt=20) # greater than

Q(ModelField__gte=20) # greater than or equal to


Comments

Popular posts from this blog

Create Desktop Application with PHP

Insert pandas dataframe into Mongodb

Add and delete columns dynamically in an HTML table