How to do it...

To illustrate the query expressions, let's create the viral video detail view and plug it into the URL configuration, as follows:

  1. Create the viral video list and detail views in views.py as follows:
# myproject/apps/viral_videos/views.py
import
logging

from django.conf import settings
from django.db import models
from django.utils.timezone import now, timedelta
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView

from .models import ViralVideo

POPULAR_FROM = getattr(settings, "VIRAL_VIDEOS_POPULAR_FROM", 500)

logger = logging.getLogger(__name__)


class ViralVideoList(ListView):
template_name = "viral_videos/viral_video_list.html"
model = ViralVideo


def viral_video_detail(request, pk):
yesterday = now() - timedelta(days=1)

qs = ViralVideo.objects.annotate(
total_views=models.F("authenticated_views") +
models.F("anonymous_views"),
label=models.Case(
models.When(total_views__gt=POPULAR_FROM,
then=models.Value("popular")),
models.When(created__gt=yesterday,
then=models.Value("new")),
default=models.Value("cool"),
output_field=models.CharField(),
),
)

# DEBUG: check the SQL query that Django ORM generates
logger.debug(f"Query: {qs.query}")

qs = qs.filter(pk=pk)
if request.user.is_authenticated:
qs.update(authenticated_views=models
.F("authenticated_views") + 1)
else:
qs.update(anonymous_views=models.F("anonymous_views") + 1)

video = get_object_or_404(qs)

return render(request, "viral_videos/viral_video_detail.html",
{"video": video})
  1. Define the URL configuration for the app as follows:
# myproject/apps/viral_videos/urls.py
from
django.urls import path

from .views import ViralVideoList, viral_video_detail

app_name = "viral_videos"


urlpatterns = [
path("", ViralVideoList.as_view(), name="viral_video_list"),
path("<uuid:pk>/", viral_video_detail,
name="viral_video_detail"),
]
  1. Include the URL configuration of the app in the project's root URL configuration as follows:
# myproject/urls.py
from django.conf.urls.i18n import i18n_patterns
from django.urls import include, path

urlpatterns = i18n_patterns(
path("viral-videos/", include("myproject.apps.viral_videos.urls", namespace="viral_videos")),
)
  1. Create a template for the viral video list view as follows:
{# viral_videos/viral_video_list.html #}
{% extends "base.html" %}
{% load i18n %}

{% block content %}
<h1>{% trans "Viral Videos" %}</h1>
<ul>
{% for video in object_list %}
<li><a href="{{ video.get_url_path }}">
{{ video.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}
  1. Create a template for the viral video detail view as follows:
{# viral_videos/viral_video_detail.html #}
{% extends "base.html" %}
{% load i18n %}

{% block content %}
<h1>{{ video.title }}
<span class="badge">{{ video.label }}</span>
</h1>
<div>{{ video.embed_code|safe }}</div>
<div>
<h2>{% trans "Impressions" %}</h2>
<ul>
<li>{% trans "Authenticated views" %}:
{{ video.authenticated_views }}
</li>
<li>{% trans "Anonymous views" %}:
{{ video.anonymous_views }}
</li>
<li>{% trans "Total views" %}:
{{ video.total_views }}
</li>
</ul>
</div>
{% endblock %}
  1. Set up the administration for the viral_videos app as follows, and add some videos to the database when you are finished:
# myproject/apps/viral_videos/admin.py
from
django.contrib import admin
from .models import ViralVideo

@admin.register(ViralVideo)
class ViralVideoAdmin(admin.ModelAdmin):
list_display = ["title", "created", "modified"]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.29.89