Tuesday, March 31, 2020

Template Variables

I am writing a simple web app, and I am struggling to access my variable attributes in one of my templates. The get_object_or_404 method returns my customized 404 error from my content_detail.html file: "No content is available." 

Here are my files:

archive/archive/urls.py:

from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('posts/', include('posts.urls')),
    path('admin/', admin.site.urls),
    path('', RedirectView.as_view(url='posts/', permanent=True)),] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

archive/posts/urls.py:

from django.urls import path

from . import views

app_name = 'posts'

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:pk>/', views.ContentDetailView.as_view(), name='content-detail'),
]

archive/posts/models.py:

import datetime

from django.db import models
from django.utils import timezone
import uuid

class Content(models.Model):
    content_head = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    class Meta:
        verbose_name_plural='contents'

    def __str__(self):
        return self.content_head

    def get_absolute_url(self):
        return reverse('detail', args=[str(self.id)])

class ContentInstance(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    content = models.ForeignKey(Content, on_delete=models.SET_NULL, null=True)
    content_body = models.TextField()

    def __str__(self):
        return f'{self.id}, {self.content.pub_date}'

archive/posts/views.py:

from django.shortcuts import render, get_object_or_404

from .models import Content, ContentInstance
from django.views import generic

def index(request):
    archive_list = Content.objects.order_by('-pub_date')[:5]
    context = {'archive_list': archive_list}
    return render(request, 'posts/index.html', context)

class ContentDetailView(generic.DetailView):
    model = Content

def content_detail_view(request, primary_key):    
    content = get_object_or_404(Content, pk=primary_key)
    return render(request, 'posts/detail.html', context={'content':content})

archive/posts/templates/posts/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Title</title>
</head>
<body>
  <h1>Heading</h1>
     {% if archive_list %}
     <ul>
      {% for content in archive_list %}
        <li><a href="/posts/{{ content.id }}/">{{ content.content_head }}</a>({{ content.pub_date }})</li>
      {% endfor %}
      </ul>
      {% else %}
      <p>No content is available.</p>
      {% endif %}
</body>
</html>

archive/posts/templates/posts/content_detail.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Title</title>
</head>
<body>
  <h1>Heading</h1>
     <ul>
     {% if content %}
       {% for contentinstance in content.contentinstance_set.all %}
        <li>{{ content.pub_date }}</li>
       <li>{{content.content_body}}</li>
       {% endfor %}
     {% else %}
     <li>No content is available</li> 
     {% endif %}
     </ul>
</body>
</html>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b86505fd-b149-48fd-a01f-b3938cb2b880%40googlegroups.com.

No comments:

Post a Comment