Midia Object has no attribute 'get_absolute_url'

Asked

Viewed 90 times

0

Has anyone ever had that problem? Attributeerror at /midia/create/

MODELS:

from django.db import models
from django.core.urlresolvers import reverse
from django.utils.text import slugify

class Midia(models.Model):
    analista = models.CharField(max_length=50)
    denuncia = models.CharField(max_length=50)
    linkDenuncia = models.CharField(max_length=500)
    ticketAxur = models.CharField(max_length=50)
    numeroFacebook = models.IntegerField(null=True)

class Meta:
    ordering = ["-analista", "-denuncia"]

    def __str__(self): 
        return self.analista

    def get_absolute_url(self):
        return reverse("midia:update", kwargs={"id": self.id})

URLS:

from django.conf.urls import url
from django.contrib import admin

from .views import (
    midia_list,
    midia_new,
    midia_edit,
    midia_remove,
    )

urlpatterns = [
    url(r'^$', midia_list, name='list'),
    url(r'^create/$', midia_new, name='create'),
    url(r'^(?P<slug>[\w-]+)/edit/$', midia_edit, name='update'),
    url(r'^(?P<slug>[\w-]+)/delete/$', midia_remove, name='delete'),
]

VIEWS:

from django.shortcuts import render
from .models import Midia
#from django.contrib import messages
from django.forms import ModelForm

from .forms import MidiaForm
from django.shortcuts import render, redirect, get_object_or_404, render_to_response
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def midia_list(request):
    queryset_list = Midia.objects.all()
    paginator = Paginator(queryset_list, 5)
    page_request_var = "página"
    page = request.GET.get(page_request_var)
    context = {
        "title": "Lista",
        "page_request_var": page_request_var,
        "object_list": queryset,
    }
    try:
        queryset = paginator.page(page)
    except PageNotAnInteger:
        queryset = paginator.page(1)
    except EmptyPage:
        queryset = paginator.page(paginator.num_pages)    
    return render(request, "midia_list.html", context)


def midia_new(request):
    form = MidiaForm(request.POST or None)
    context = {
            "form": form,
    }
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user = request.user
        print (form.cleaned_data.get("analista"))
        instance.save()
        # message success
        #messages.success(request, "Criado com sucesso!")
        return HttpResponseRedirect(instance.get_absolute_url())

    return render(request, "midia_form.html", context)




def midia_edit(request, slug=None):
    instance = get_object_or_404(Midia, slug=slug)
    form = MidiaForm(request.POST or None, instance=instance)
    context = {
        "title": instance.title,
        "instance": instance,
        "form": form,
    }
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        #messages.success(request)
        return HttpResponseRedirect(instance.get_absolute_url())

    return render(request, "midia_form.html", context)

def midia_remove(request, slug=None):
    instance = get_object_or_404(Midia, slug=slug)
    if request.method == "POST":
        midia.delete()
        return redirect('list')
    return render(request, 'midia_delete.html', {'midia': midia})

HTML:

{% extends 'base.html' %}

{% block title %}
Listagem de midiasocial
{% endblock title %}

{% block tituloPagina %}
<center>Listagem midia social</center>
{% endblock tituloPagina %}

{% block content %}

<table class="table">
  <thead>
    <tr>
      <th>Analista</th>
      <th>Origem Denúncia</th>
      <th>Link Denuncia</th>
      <th>Ticket Axur</th>
      <th>Numero Facebook</th>
      <th>Ação</th>
    </tr>
  </thead>
  <tbody>
  {% for obj in object_list %}
    <tr>
      <td>{{ midia.analista }}</td>
      <td>{{ midia.denuncia }}</td>
      <td>{{ midia.linkDenuncia }}</td>
      <td>{{ midia.ticketAxur }}</td>
      <td>{{ midia.numeroFacebook }}</td>
      <td>
       <!-- <a href="{% url 'update' pk=midia.pk %}">
          <span class="glyphicon glyphicon-pencil"></span>
        </a> -->
        <a href="{% url 'delete' instance.id %}">
          <span class="glyphicon glyphicon-trash"></span>
        </a>
      </td>
    </tr>



     <div class="pagination">
    <span class="step-links">
        {% if object_list.has_previous %}
            <a href="?page={{ object_list.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ object_list.number }} of {{ object_list.paginator.num_pages }}.
        </span>

        {% if object_list.has_next %}
            <a href="?page={{ object_list.next_page_number }}">next</a>
        {% endif %}
    </span>
    </div>



  {% endfor %}
  </tbody>
</table>
<a href="{% url 'create' %}">
<button type="submit" class="btn btn-primary">
    Cadastrar
</button>
</a>

{% endblock content %}

FORMS:

from django import forms
from .models import Midia

class MidiaForm(forms.ModelForm):
    class Meta:
        model = Midia
        fields = [
            'analista', 
            'denuncia', 
            'linkDenuncia', 
            'ticketAxur', 
            'numeroFacebook',
        ]

1 answer

0

I don’t know if you put the wrong code in the question, but the indentation is wrong, the right one would be:

class Midia(models.Model):

    analista = models.CharField(max_length=50)
    denuncia = models.CharField(max_length=50)
    linkDenuncia = models.CharField(max_length=500)
    ticketAxur = models.CharField(max_length=50)
    numeroFacebook = models.IntegerField(null=True)

    class Meta:
        ordering = ["-analista", "-denuncia"]

    def __str__(self): 
        return self.analista

    def get_absolute_url(self):
        return reverse("midia:update", kwargs={"id": self.id})

If you can, enter the error that appears in Terminal, the error line, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.