Я пытаюсь показать общее количество контактов из этой модели .py
import arrow
from django.db import models
from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel
from django_countries.fields import CountryField
from phonenumber_field.modelfields import PhoneNumberField
from django.urls import reverse
from django.conf import settings
class Contact(TimeStampedModel):
name = models.CharField("Name of Contact", max_length=255)
first_name = models.CharField("First name", max_length=255, null=True)
last_name = models.CharField("Last name", max_length=255, null=True)
email = models.EmailField(unique=True, null=True)
phone = PhoneNumberField(null=True, unique=True)
slug = AutoSlugField("Contact Address",
unique=True,
always_update=False,
populate_from="name")
description = models.TextField("Description", blank=True)
class Firmness(models.TextChoices):
UNSPECIFIED = "unspecified", "Unspecified"
SOFT = "soft", "Soft"
SEMI_SOFT = "semi-soft", "Semi-Soft"
SEMI_HARD = "semi-hard", "Semi-Hard"
HARD = "hard", "Hard"
# Other Fields Here...
firmness = models.CharField("Firmness", max_length=20, choices=Firmness.choices, default=Firmness.UNSPECIFIED)
country_of_origin = CountryField("Country of Origin", blank=True)
creator = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
created_on = models.DateTimeField("Created on", auto_now_add=True, null=True)
is_active = models.BooleanField(default=False)
def __str__(self):
return self.name
def get_absolute_url(self):
""""Return absolute URL to the Contact Detail page."""
return reverse('contacts:detail', kwargs={"slug": self.slug})
@property
def created_on_arrow(self):
return arrow.get(self.created_on).humanize()
class Meta:
ordering = ["-created_on"]
И у меня есть view.py
from django.shortcuts import render
from .models import Contact
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import (
ListView,
DetailView,
CreateView,
UpdateView
)
class ContactListView(ListView):
model = Contact
class ContactDetailView(DetailView):
model = Contact
def total(request):
total_contacts = Contact.objects.all()
show_total_contacts = total_contacts.objects.count()
return render(
request, 'pages/home.html',
{'total_contacts': total_contacts,
'show_total_contacts': show_total_contacts}
)
class ContactCreateView(LoginRequiredMixin, CreateView):
model = Contact
fields = [
'name',
'description',
'firmness',
'country_of_origin',
'first_name',
'last_name',
'email',
'phone',
'is_active'
]
def form_valid(self, form):
form.instance.creator = self.request.user
return super().form_valid(form)
class ContactUpdateView(LoginRequiredMixin, UpdateView):
model = Contact
fields = [
'name',
'description',
'firmness',
'country_of_origin',
'first_name',
'last_name',
'email',
'phone',
'is_active'
]
action = "Update"
, и это мой urls.py
# everycheese/cheeses/urls.py
from django.urls import path
from . import views
from django.conf.urls.static import static
from django.conf import settings
app_name = "contacts"
urlpatterns = [
# URL Pattern for the ContactListView
path(
route='',
view=views.ContactListView.as_view(),
name='list'
),
# URL Pattern for the ContactCreateView
path(
route='add/',
view=views.ContactCreateView.as_view(),
name='add'
),
# URL Pattern for the ContactUpdateView
path(
route='<slug:slug>/update/',
view=views.ContactUpdateView.as_view(),
name='update'
),
# URL Pattern for the ContactDetailView
path(
route='<slug:slug>/',
view=views.ContactDetailView.as_view(),
name='detail'
),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Я только что закончил Two S coop Django 3 Cra sh Учебник. И я многому оттуда научился, но на самом деле он не вдавался в подробности о том, как обрабатывать такие вещи, как функции подсчета или более продвинутые вещи, такие как django rest framework.
Спасибо, что нашли время мне помочь ...