Menu
CoddyTech

Trang hồ sơ

Thuộc phần Người dùng và tài khoản trong hành trình Django của Coddy. Bài học 14 trên 16.

Trong chương này, bạn sẽ thêm các tính năng dựa trên những gì đã biết và cần ít trợ giúp hơn. Đầu tiên, hãy tạo một trang của riêng bạn: ai đang đăng nhập và họ đã viết bao nhiêu ghi chú. Mọi thông tin cần thiết đều có trong người dùng đang đăng nhập: tên người dùng và các ghi chú của họ thông qua related_name của trường chủ sở hữu.

Để hiển thị số hàng, hãy yêu cầu cơ sở dữ liệu đếm số lượng. Trên một trang web thư viện, author.books.count() chạy một câu lệnh SELECT COUNT(*). len(author.books.all()) cho cùng kết quả, nhưng trước tiên sẽ tải tất cả sách.

Trong tiếng Anh, cần viết 1 book nhưng 2 books. Như ở phần đầu tiên, bộ lọc pluralize thêm s trừ khi số lượng là 1: book{{ count|pluralize }}. Bạn có thể đặt một đuôi khác trong dấu ngoặc kép: box{{ count|pluralize:"es" }}.

challenge icon

Thử thách

Trung bình

Thêm trang hồ sơ.

  1. Trong notes/views.py, viết profile: hiển thị notes/profile.html cùng với số ghi chú của người dùng.
  2. Trong notes/urls.py, thêm profile/ với tên profile.
  3. Tạo profile.html kế thừa notes/base.html. Chỉ hiển thị tên người dùng trong thẻ <h1>, sau đó là You have 2 notes. Dùng pluralize để hiển thị You have 1 note. khi có một ghi chú.
  4. Trong base.html, liên kết Profile đến trang đó.

Với tư cách là ada, hãy nhấp vào Hồ sơ: Bạn có 2 ghi chú.

Tự làm thử

from django.contrib.auth import login
from django.contrib.auth.decorators import login_not_required, permission_required
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect, render

from .forms import NoteForm
from .models import Note


def note_list(request):
    notes = Note.objects.filter(owner=request.user)
    return render(request, "notes/note_list.html", {"notes": notes})


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


def note_create(request):
    if request.method == "POST":
        form = NoteForm(request.POST)
        if form.is_valid():
            note = form.save(commit=False)
            note.owner = request.user
            note.save()
            return redirect("notes:detail", pk=note.pk)
    else:
        form = NoteForm()
    return render(request, "notes/note_form.html", {"form": form})


def note_edit(request, pk):
    note = get_object_or_404(Note, pk=pk, owner=request.user)
    if request.method == "POST":
        form = NoteForm(request.POST, instance=note)
        if form.is_valid():
            form.save()
            return redirect("notes:detail", pk=note.pk)
    else:
        form = NoteForm(instance=note)
    return render(request, "notes/note_form.html", {"form": form, "note": note})


def note_delete(request, pk):
    note = get_object_or_404(Note, pk=pk, owner=request.user)
    if request.method == "POST":
        note.delete()
        return redirect("notes:list")
    return render(request, "notes/note_confirm_delete.html", {"note": note})


@login_not_required
def signup(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect("notes:list")
    else:
        form = UserCreationForm()
    return render(request, "notes/signup.html", {"form": form})


def stats(request):
    if not request.user.is_staff:
        raise PermissionDenied
    context = {"user_count": User.objects.count(), "note_count": Note.objects.count()}
    return render(request, "notes/stats.html", context)


@permission_required("notes.view_note", raise_exception=True)
def all_notes(request):
    notes = Note.objects.select_related("owner")
    return render(request, "notes/all_notes.html", {"notes": notes})

Tất cả bài học trong Người dùng và tài khoản

Tự luyện tập: Playground Django