Friday, April 3, 2020

Error in formatting: AttributeError: 'UUID' object has no attribute 'int'

When trying to render my content_detail.html template (printed below), I am receiving the following error:

  • /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/uuid.py in __init__
    1.                 raise ValueError('badly formed hexadecimal UUID string')
    VariableValue
    bytes
    None
    bytes_le
    None
    fields
    None
    hex
    '2'
    int
    None
    is_safe
    <SafeUUID.unknown: None>
    self
    Error in formatting: AttributeError: 'UUID' object has no attribute 'int'
    version
    None

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
from django.urls import reverse

# Create your models here.

class Content(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('content-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)
    text = models.TextField()

    def __str__(self):
        return '{0} ({1})'.format(self.id, self.content.title)

archive/posts/views.py:
from django.shortcuts import render

# Create your views here.

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

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

class ContentDetailView(generic.DetailView):
    model = Content

archive/posts/templates/posts/content_detail.py:

{% extends "base_generic.html" %}

{% block content %}
  <h1>Heading</h1>
  <h2>{{ content.title }}</h2>
  <p>{{ content.pub_date }}</p>
  <ul>
  {% for copy in content.contentinstance_set.all %} 
  <li><{{ copy.text }}></li>
  {% endfor %}
  </ul>
{% endblock %}


--
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/a55a7579-1cd1-4003-bb4e-2398ace9ed0a%40googlegroups.com.

No comments:

Post a Comment