STAY INFORMED
following content serves as a personal note and may lack complete accuracy or certainty.

Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Unix Commands
npm Commands
Vim Commands
Git Note
Useful Figma Shortcut Keys

3 minute read

Setup

check data -> data modeling -> create field -> create / update model -> apply to django

# models.py
class Menu(models.Model):
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=100)
    price = models.IntegerField()
    img = models.CharField(max_length=255)

    # dt_created = models.DateTimeField(verbose_name="Date Created", auto_now_add=True)
    # verbose_name: convert to data readable
    # auto_now_add: usually used for created

    def __str__(self):
        return self.name

error_message: validation depending on the key.

python manage.py makemigrations
python manage.py migrate

migrations are saved in migrations/0001_initial.py.

More commands

python manage.py showmigrations
python manage.py sqlmigrate [project-name] [migrations-file-number]

Field Types

Field Name Description Attributes
CharField String field with a defined maximum length max_length (maximum number of characters)
TextField String field with no defined maximum length  
EmailField String field similar to CharField but checks for email format max_length=254 (default)
URLField String field similar to CharField but checks for URL format max_length=200 (default)
BooleanField Field with True or False values  
IntegerField Field for integer values  
FloatField Field for floating-point values  
DateField Field for date values auto_now (updates on modify), auto_now_add (sets on create)
TimeField Field for time values auto_now, auto_now_add
DateTimeField Field for date and time values auto_now, auto_now_add

CRUD

Create

Execute django shell

python manage.py shell

Import data table

from [app-name].models import Menu

Create data

Menu.objects.create(name="chicken",
... description="chicken description",
... price=10,
... img="foods/images/chicken.jpg")

<Menu: chicken>

Read

Import data table

from [app-name].models import Menu

Read all data in table

Menu.objects.all()

Read all data detail in table

Menu.objects.all().values()
or
Menu.objects.all().values('price')

Ascending / Descending order by price

Menu.objects.order_by('price')

Count number of row

rows = Menu.objects.count()

Read specific data

Filter

It can return more than 2 data.

Menu.objects.filter(name__contains="b") # find item contains 'b', case sensetive
Menu.objects.filter(name__icontains="b") # find item contains 'b'

Menu.objects.filter(name__exact="b") # find item 'b', case sensetive
Menu.objects.filter(name__iexact="b") # find item 'b'

Menu.objects.filter(price__range=(1, 15)) # find item with price between 1 and 15

start_date = datetime.date(2023,8,27)
end_date = datetime.date(2024,6,27)
data = Menu.objects.filter(pub_date__range=(start_date,end_date)) # find item published date between 2023-06-27 and 2024-06-27
Get

It can return only one data. If there are more than 2, gives an error.

Menu.objects.get(id=1) # find item with id 1

More info of Field Lookup

Update

Need to assign data to a variable

data = Menu.objects.get(id=1)

and update it

data.name = "fried chicken"

# save is required
data.save()

Delete

Need to assign data to a variable and delete it.

data = Menu.objects.get(id=1)
data.delete()

Code

# urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('menu/<int:pk>/', views.food_detail, name='food-detail'),
    path('menu/', views.index),
]
#views.py
from .models import Menu

def index(request):
    today = datetime.today().date()
    context = dict()
    context["date"] = today
    menus = Menu.objects.all()
    context["menus"] = menus
    return render(request, 'foods/index.html', context=context)


def food_detail(request, pk):
    context = dict()
    menu = Menu.objects.get(id=pk)
    context["menu"] = menu

    return render(request, 'foods/detail.html', context=context)
#index.py
{% extends './base.html' %}
{% load static %}

{% block date-block%}
<div>{{date}}</div>
{% endblock date-block%}

{% block food-container%}
  {% for menu in menus %}
    <div class="food">
      <img src={% get_static_prefix %}{{ menu.img }}/>
      <div class="info">
        <h3>{{menu.name}}</h3>
        <p>{{menu.description|linebreaksbr}}</p>
        <a href={% url 'food-detail' food.id %}>view</a>
      </div>
    </div>
  {% endfor %}
{% endblock food-container%}

linebreaksbr: a template filter used to convert line breaks in plain text into HTML line break br tags. This is useful when you want to preserve the formatting of text that contains line breaks when rendering it in an HTML template.

Admin tools

First, create an account

python manage.py createsuperuser

# enter your info
Username (leave blank to use 'henrychung'):
Email address:
Password:
Password (again):
Superuser created successfully.

You can access the administration website via http://127.0.0.1:8000/admin/

You can handle it in admin.py

# admin.py
from django.contrib import admin
from foods.models import Menu
# Register your models here.

admin.site.register(Menu)

des1

You can do CRUD the database on this page.

Tags:

Categories:

Updated: