Skillversitas

Creating a First App

The first app is the Note model plus the admin plus the 2 view: the startapp notes_app the 7 file, the Note the 2 field (the title the body, the created the auto), the makemigrations plus the migrate the table, the admin.site.register the CRUD the 1 line, and the note_list the note_detail the 2 view the 1 template. The urls.py is the 2 level (the app the path, the project the include), and the template is the base plus the block (the 1 file, the no the repeat). This chapter covers the Note (the 2 field, the save), the migration (the table), the admin (the register), the 2 view (the list, the detail), and the urls (the include, the int the pk).

The Note: The 2 field

# the NOTE (the 2 field, the 1 auto):
from django.db import models

class Note(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    created = models.DateTimeField(
        auto_now_add=True)   # the no the
                              #   the touch

    class Meta:
        ordering = ["-created"]   # the new
                                   #   the first

    def __str__(self):
        return self.title
#   the the 3: the model the class,
#     the field the column, the Meta
#     the option (the ordering, the
#     the verbose)
#   the the shell the 1: the
#     manage.py shell the create
python manage.py shell
#   the 1: the Note.objects.create(
#     title="the 1", body="the text")
#   the 2: the Note.objects.all()
#   the 3: the n = Note.objects.get(
#     pk=1); n.title = "the 2";
#     n.save()   #   the UPDATE

The Note is the 2 field: the title (the CharField the max_length, the varchar), the body (the TextField, the text), and the created (the DateTimeField the auto_now_add, the no the touch) - the Meta.ordering is the -created (the new the first, the list the no the order_by), and the __str__ is the admin the label. The shell is the test: the manage.py shell the create (the Note.objects.create the INSERT), the all (the SELECT), and the get plus the save (the UPDATE, the no the INSERT the 2) - the migration is the table: the makemigrations the diff (the 1 file the no the SQL), the migrate the SQL the DB, and the admin is the register the next.

The admin and the 2 view

# the ADMIN (the CRUD, the 1 line):
from django.contrib import admin
from .models import Note

@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
    list_display = ("title", "created")
    list_filter = ("created",)
    search_fields = ("title", "body")
#   the /admin the list the change
#     the delete (the auth the require)

# the VIEW (the 2, the list the detail):
from django.shortcuts import (
    render, get_object_or_404)

def note_list(request):
    notes = Note.objects.all()
    return render(request,
        "notes/list.html", {"notes": notes})

def note_detail(request, pk):
    note = get_object_or_404(Note, pk=pk)
    return render(request,
        "notes/detail.html", {"note": note})

The admin is the register: the @admin.register(Note) the 1 line (the /admin the list the change the delete the auto), and the ModelAdmin is the 3 option (the list_display the column, the list_filter the sidebar, the search_fields the box) - the auth is the require (the createsuperuser the 1 user, the login the no the 403). The 2 view is the list plus the detail: the note_list the all (the QuerySet the context), the note_detail the pk (the int the pk the urls the capture), and the get_object_or_404 is the safe (the no the DoesNotExist the 500, the 404 the page) - the template is the base (the {% block %}), the list (the {% for %}), the detail (the note.title), and the urls is the next: the app the path, the project the include.

The urls and the template

# the URLS (the 2 level, the app the
#   project):
#   the app: notes_app/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.note_list,
        name="note-list"),
    path("<int:pk>/", views.note_detail,
        name="note-detail"),
]
#   the <int:pk> the capture (the 1,
#     the 2, the no the "abc")
#   the name the reverse (the
#     the {% url %} the template)

#   the project: notes/urls.py
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("notes/", include(
        "notes_app.urls")),   #   the
                                #   the app's
]
#   the /notes/ the app's, the
#     /notes/1/ the detail

# the TEMPLATE (the base, the block):
#   the base.html (the 1, the no the
#     the repeat):
# <!doctype html>
# <html><head><title>Notes</title>
# </head><body>
# <nav><a href="{% url
#   'note-list' %}">Notes</a></nav>
# {% block content %}{% endblock %}
# </body></html>
#   the list.html (the {% extends %}):
# {% extends "base.html" %}
# {% block content %}
# {% for note in notes %}
# <a href="{% url 'note-detail'
#   note.pk %}">{{ note.title }}</a>
# {% endfor %}
# {% endblock %}

The urls is the 2 level: the app the path (the path("", note_list), the <int:pk> the capture, the name the reverse), and the project the include (the path("notes/", include("notes_app.urls")) the prefix) - the /notes/ is the list, the /notes/1/ is the detail, and the {% url "note-list" %} is the reverse (the name the URL, the no the hardcode). The template is the base plus the 2: the base.html is the 1 (the nav, the {% block content %}), the list.html is the {% extends %} (the {% for %} the note), and the detail.html is the {% extends %} (the note.title, the note.body) - the runserver is the test: the /notes/ the list (the 1 the shell the create), the /notes/1/ the detail, and the /admin the CRUD (the createsuperuser the login) - the rule is the one: the Note the 2 field, the migration the table, the admin the register, the 2 view, the urls the include, and the template the base - the next chapter's model is the relation the ForeignKey the M2M the field the full.

The Working Rules

  • The Note is the 2 field, the Meta the ordering. The title the CharField (the max_length), the body the TextField, and the created the auto_now_add (the no the touch) - the Meta.ordering the -created (the new the first), the __str__ the admin's label, and the shell the create the all the get the save (the INSERT, the SELECT, the UPDATE).
  • The migration is the table, the 2 command. The makemigrations the diff (the 1 file, the no the SQL, the no the DB), and the migrate the SQL the DB (the table, the column, the index) - the sqlmigrate the read (the no the run, the debug), and the showmigrations the state (the applied, the no the apply).
  • The admin is the register, the 3 option. The @admin.register(Note) the 1 line (the CRUD the auto), and the ModelAdmin the list_display the list_filter the search_fields - the auth the require (the createsuperuser, the login, the no the 403), and the /admin the no the view the template the form.
  • The urls is the 2, the template the base. The app the path (the <int:pk> the capture, the name the reverse), and the project the include (the prefix the notes/) - the {% url %} the no the hardcode, the base the 1 (the block), the list the detail the {% extends %}, and the runserver the /notes/ the /notes/1/ the /admin.

Summary

The first app is the 5: the Note model (the title the CharField, the body the TextField, the created the auto_now_add, the Meta.ordering the -created), the migration (the makemigrations the diff, the migrate the SQL, the table the column), the admin (the @admin.register(Note) the 1 line, the ModelAdmin the list_display the list_filter the search_fields, the auth the require), the 2 view (the note_list the all, the note_detail the get_object_or_404), and the urls plus the template (the app the path the <int:pk>, the project the include, the base the block, the {% url %} the reverse). The runserver is the test (the /notes/ the list, the /notes/1/ the detail, the /admin the CRUD) - the next chapter's models is the field the full (the ForeignKey, the M2M, the OneToOne, the Boolean, the Integer, the Image, the File) and the relation the related_name the select_related the prefetch_related the N+1 the fix.