Menu
CoddyTech

Chỉ ghi chú của bạn

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 9 trên 16.

Mỗi ghi chú đều có chủ sở hữu, nhưng danh sách vẫn hiển thị Note.objects.all(): ada đọc nhật ký của bob. Danh sách chỉ nên hiển thị ghi chú của người đang gửi yêu cầu.

Bạn lọc theo ForeignKey bằng một đối tượng, không phải tên. Một trang web thư viện liệt kê sách của một tác giả:

author = Author.objects.get(name="Ursula K. Le Guin")
books = Book.objects.filter(author=author)

Django so sánh id của tác giả. Truyền tên vào, filter(author="Ursula K. Le Guin"), sẽ gây ra ValueError. Để khớp theo tên tác giả, hãy thêm hai dấu gạch dưới vào sau khóa: filter(author__name="Ursula K. Le Guin").

Ở đây, đối tượng cần so sánh là khách truy cập, nên bạn không cần tra cứu ai cả. Django đã thêm người dùng đã đăng nhập vào mọi yêu cầu. Đừng bao giờ lấy người dùng từ URL hay trường trong biểu mẫu, vì bất kỳ ai cũng có thể thay đổi chúng.

challenge icon

Thử thách

Dễ

Trong note_list, chỉ liệt kê ghi chú của người dùng đã đăng nhập.

Đăng nhập bằng tài khoản ada (mật khẩu coddy-pass-123): danh sách hiển thị Shopping list và Ideas, không hiển thị Bob's diary.

Tự làm thử

from django.contrib.auth import login
from django.contrib.auth.decorators import login_not_required
from django.contrib.auth.forms import UserCreationForm
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.all()
    return render(request, "notes/note_list.html", {"notes": notes})


def note_detail(request, pk):
    note = get_object_or_404(Note, pk=pk)
    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)
    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)
    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})
quiz iconTự kiểm tra

Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.

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

Tự luyện tập: Playground Django