Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: CRUD with Person | Working with Database
Django: First Dive

book
Challenge: CRUD with Person

Note

The presented code for the task is part of the framework and cannot work separately, so errors will be received when you try to Run Code. Use the Submit Task button to solve this task.

Aufgabe

Swipe to start coding

You have already implemented Model class:

python
class Dog(models.Model):
name = models.CharField(max_length=30)
age = models.IntegerField()
weight = models.FloatField()

object = models.Manager()

Write 5 view functions:

  1. dog_list(request) should display the list of dogs on the HTML page like following:

    html
    <h1>Dog {id}</h1>
    <p>Name: {name}</p>
    <p>Age: {age}</p>
    <p>Weight: {weight}</p>
  2. dog_get(request, pk) should display the information about the selected Dog by pk (id).

  3. dog_create(request) should create the new Dog with the name "Unknown", age 0, and weight 1.1.

  4. dog_update(request, pk) should update the Dog's name to "Updater", age += 1, and weight += 0.2.

  5. dog_delete(request, pk) should delete the existing dog by id.

Connect views to URLs

URLView
dogs/dog_list
dogs/id/dog_get
dogs/id/update/dog_update
dogs/id/delete/dog_delete
dogs/create/dog_create

Lösung

from django.urls import path
from django.http import HttpResponse
from new_app.models import Dog


def dog_list(request):
dogs = Dog.objects.all()
html = ""

for dog in dogs:
html += f"<h1>Dog {dog.id}</h1>"
html += f"<p>Name: {dog.name}</p>"
html += f"<p>Age: {dog.age}</p>"
html += f"<p>Weight: {dog.weight}</p>"

return HttpResponse(html)


def dog_create(request):
new_dog = Dog()

new_dog.name = "Unknown"
new_dog.age = 0
new_dog.weight = 1.1

new_dog.save()

return HttpResponse("<h1>Complete!</h1>")


def dog_get(request, pk):
dog = Dog.objects.get(pk=pk)

html = f"<h1>Dog {dog.id}</h1>"
html += f"<p>Name: {dog.name}</p>"
html += f"<p>Age: {dog.age}</p>"

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 4
from django.urls import path
from django.http import HttpResponse
from new_app.models import Dog


def dog_list(request):
dogs = ___.___.___()
___ = ""

for dog in dogs:
html += f"<h1>Dog {___.id}</h1>"
html += f"<p>Name: {dog.___}</p>"
html += f"<p>Age: {dog.___}</p>"
html += f"<p>Weight: {dog.___}</p>"

return ___(___)


def dog_create(request):
___ = Dog()

new_dog.___ = "Unknown"
new_dog.age = 0
___.weight = ___

new_dog.___()

return HttpResponse("<h1>Complete!</h1>")


def dog_get(request, ___):
dog = Dog.objects.___(___=pk)

html = f"<h1>Dog {dog.id}</h1>"
html += f"<p>Name: {dog.name}</p>"
html += f"<p>Age: {dog.age}</p>"
html += f"<p>Weight: {dog.weight}</p>"

return HttpResponse(html)


def dog_update(request, pk):
dog = Dog.objects.___(pk=pk)
dog.___ = "Updater"
dog.age ___ 1
dog.weight ___ 0.2

dog.___()

return HttpResponse(f"<h1>Update Dog (id {dog.___})</h1>")


def dog_delete(request, pk):
dog = Dog.objects.get(pk=pk)
dog_id = dog.id
dog.___()

return HttpResponse(f"<h1>Delete Dog (id {___})</h1>")


urlpatterns = [
path("dogs/", ___, name="dog-list"),
path("dogs/___/", ___, name="dog-get"),
path("dogs/<int:pk>/___/", ___, name="dog-update"),
path("dogs/<int:pk>/___/", ___, name="dog-delete"),
path("dogs/___/", ___, name="dog-create"),

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

We use cookies to make your experience better!
some-alt