Halaman Profil
Termasuk bagian Pengguna dan Akun dalam perjalanan Django di Coddy. Pelajaran 14 dari 16.
Di bab ini, kamu menambahkan fitur dengan pengetahuanmu dan sedikit bantuan. Pertama, buat halamanmu sendiri: siapa yang sedang masuk dan berapa banyak catatan yang mereka tulis. Semua yang dibutuhkan ada pada pengguna yang sedang masuk: nama pengguna dan catatan mereka melalui related_name pada field pemilik.
Untuk menampilkan jumlah baris, minta database menghitungnya. Di situs library, author.books.count() menjalankan satu SELECT COUNT(*). len(author.books.all()) memberikan jumlah yang sama, tetapi memuat setiap buku terlebih dahulu.
Dalam bahasa Inggris, gunakan 1 book, tetapi 2 books. Seperti di bagian pertama, filter pluralize menambahkan s kecuali jumlahnya 1: book{{ count|pluralize }}. Akhiran lain ditulis di dalam tanda kutip: box{{ count|pluralize:"es" }}.
Tantangan
SedangTambahkan halaman profil.
- Di
notes/views.py, tulisprofile: tampilkannotes/profile.htmldengan jumlah catatan milik pengguna. - Di
notes/urls.py, tambahkanprofile/dengan namaprofile. - Buat
profile.htmlyang mewarisinotes/base.html. Tampilkan nama pengguna saja di dalam<h1>, laluYou have 2 notes.Gunakanpluralizeagar untuk satu catatan tertulisYou have 1 note. - Di
base.html, tautkanProfileke halaman tersebut.
Sebagai ada, klik Profile: You have 2 notes.
Coba sendiri
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})
Semua pelajaran di Pengguna dan Akun
Latihan sendiri: Playground Django