Menu
CoddyTech

Đường liên kết công khai

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

Ghi chú được chia sẻ cần có một địa chỉ mà bất kỳ ai cũng có thể mở, dù đã đăng nhập hay chưa, và chỉ hiển thị ghi chú đó. Trang của chủ sở hữu có các nút Edit và Delete, còn trang này thì không.

Bạn đã dùng hết các thành phần rồi. Trang cần quyết định ba điều: ai có thể mở trang, trang có thể tìm những ghi chú nào và trang dùng mẫu nào.

Hãy kiểm tra trang với ba người: chủ sở hữu, người dùng khác và khách chưa đăng xuất.

challenge icon

Thử thách

Trung bình

Hiển thị ghi chú được chia sẻ trên một trang công khai.

  1. Trong notes/urls.py, thêm shared/<int:pk>/ với tên shared cho shared_note.
  2. Trong notes/views.py, viết shared_note với @login_not_required. Hàm này chỉ tìm ghi chú được chia sẻ; các ghi chú khác trả về lỗi 404.
  3. Hiển thị notes/shared_note.html với tiêu đề, nội dung và chủ sở hữu, không có liên kết Edit hoặc Delete.
  4. Trong note_detail.html, ở phần ghi chú được chia sẻ, liên kết Share link đến notes:shared.

Đăng xuất rồi mở /shared/2/: Pancakes sẽ hiển thị, còn /shared/1/ trả về lỗi 404.

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})


def profile(request):
    note_count = request.user.notes.count()
    return render(request, "notes/profile.html", {"note_count": note_count})
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