Python Forum
<title> django page title dynamic and other field (not working) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: <title> django page title dynamic and other field (not working) (/thread-35446.html)



<title> django page title dynamic and other field (not working) - lemonred - Nov-03-2021

Hello , my name luigi from italy age 39 old year .. my studing hobby web programming for django web framework.
my problem not work in template <title>blcok code and other row.

only model photo .. not working model Album in album_detail.html

showing :

{{album.title}}

idea? how to fixed ?? django 3.2

"""
album.html 
"""
{% extends "site/common.album.html" %}
{% load static %}
{% block title %} Album {% endblock %}
{% block content %}
<div class="row" >
{% for albumData in album  %}
  <div class="col-sm-4"  style="padding-top:30px;">
    <div class="card"> 
      <div class="card-body">
        <h5 class="card-title">{{albumData.titolo }}</h5>
        <p class="card-text">{{albumData.descrizione }}</p>
        <a href="{{albumData.id }}/">Accedi</a>
      </div>
    </div>
  </div>
{% empty %}
<div>No album</div>
{% endfor %}
</div>
 
{% endblock content %}
 
""" 
album_detail.html 
"""
 
{% extends "site/common.photo.html" %}
{% load static %}
 
{% block title %} Foto {% endblock title %}
{% block content %}
<h1 class="display-1 text-center">Foto</h1>
<h3>{{album.titolo}}</h3>
<div id="photo">
          <ul class="album album_1">
            {% for photoData in photo %}
            <li><figure class="figure">
              <a href="/public/{{photoData.foto.large }}" class="big"><img src="/public/{{photoData.foto.thumbnail }}" class="figure-img img-fluid rounded big" alt="{{photoData.titolo }}" title="{{photoData.descrizione }}"></a>
              <figcaption class="figure-caption text-center">{{photoData.descrizione }}</figcaption>
            </figure>
          </li>
          {% empty %}
          <div>No photo</div>
          {% endfor %}
        </ul>  
</div>
</div>
{% endblock content %}

"""
views.py
"""
from django.shortcuts import render
from .models import Album, Photo
 
def view_album(request):
    album = Album.objects.all().order_by('creato')
    context = {'album': album}
    return render(request, "album.html", context)
 
def view_photo(request, album_id):
    photo = Photo.objects.filter(album_id=album_id).all().order_by('creato')
    context = {'photo': photo}
    return render(request, "album_detail.html", context)



RE: <title> django page title dynamic and other field (not working) - lemonred - Nov-04-2021

Problem solved.

views.py
def view_album(request):
    album = Album.objects.all().order_by('creato')
    return render(request, 'album.html', {'album': album})


def view_photo(request,  album_slug):
    photo = Photo.objects.filter(album__slug=album_slug)
    album = Album.objects.filter(slug=album_slug).order_by('creato')
    return render(request, 'album_detail.html', {'photo': photo, 'album': album})
models.py
from django.db import models
from django.utils.text import slugify
from django.urls import reverse
STATO = (
    (0,"Bozza"),
    (1,"Publico")
)

class Pages(models.Model):

    id = models.AutoField(primary_key=True)
    autore = models.ForeignKey(
        "auth.User",
        on_delete=models.CASCADE,
    )
    titolo = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, unique=True)
    contenuto = models.TextField()
    stato = models.IntegerField(choices=STATO, default=0)
    creato = models.DateTimeField(auto_now_add=True)
    modificato = models.DateTimeField(auto_now= True)

    def __str__(self):
        return self.titolo

    def get_absolute_url(self):
        return reverse('pages', kwargs={"id": self.id,'slug': self.slug})
        
    def save(self, *args, **kwargs):
        self.slug = slugify(self.titolo)
        super().save(*args, **kwargs)
        
    class Meta:
        ordering = ['-creato']