1

I am trying to update one field of my model, I have overridden the get_success_url method in the updateview, but it is not working.

class MensajeApropiado(UpdateView):
    model = Mensaje
    form_class = FormMensaje
    template_name = 'foro/msjCensura.html'

    def get_success_url(self):
        return reverse_lazy('mensajes', args=(self.object.tema.id,))

urls.py

from django.contrib.auth.decorators import login_required as LR
url(r'^mensajes/(?P<pk>\d+)/$', ListaMensajes.as_view(),  name="mensajes"),
url(r'^msjcensura/(?P<pk>\d+)/', LR(MensajeApropiado.as_view()), name="censura"),

This is the template:

{% extends "base.html" %}
{% load i18n %}


{% block content %}



Fecha en Perú: {% now "m-d-Y" %}

<div>
    <a href="{% url 'home' %}">Foros</a> > <a
        href="{% url 'temas' mensaje.tema.foro.id %}">{{ mensaje.tema.foro.titulo }}</a>
    > <a href="{% url 'mensajes' mensaje.tema.id %}">{{ mensaje.tema.titulo }}</a> > Censura del mensaje
</div>


{% if mensaje.tema.autor.user.id == request.user.id and request.user.is_authenticated %}
    Tema: {{ mensaje.tema.titulo }} - {{ mensaje.tema.fecha|date:"M-d-Y" }}
    - Autor: <a href="{% url 'editar_perfil' request.user.id %}">{{ mensaje.tema.autor }} </a>
{% else %}
    Tema: {{ mensaje.tema.titulo }} - {{ mensaje.tema.fecha|date:"M-d-Y" }}
    - Autor: <a href="{% url 'detalle_usuario' mensaje.autor.id %}">{{ mensaje.autor }} </a>
{% endif %}
<br><br>

<div id="no-more-tables">

    <table style='table-layout:fixed;width:100%' class="col-md-12 table-bordered table-striped table-condensed cf">

        <thead class="cf">
        {% with mensaje.profile_data as pdata %}
            <tr>

                <th style='width: 15%;'>
                    Autor

                </th>
                <th class="celdacontenido" style='width: 85%;'>
                    {{ mensaje.fecha|date:"M, d-Y, h:i a" }}

                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
            <td data-title='Autor'>
            {% if mensaje.autor.id == request.user.id and request.user.is_authenticated %}
                <a href="{% url 'editar_perfil' request.user.id %}"> {{ mensaje.autor }} </a><br>
            {% else %}
                <a href="{% url 'detalle_usuario' mensaje.autor.id %}"> {{ mensaje.autor }} </a><br>
            {% endif %}

            {% if request.user.is_staff %} Administrador<br>
            {% else %}
                Miembro<br>
            {% endif %}

            {% if  pdata.1 %}
                <img border="0" alt="Profile Pic" src="/media/{{ pdata.1 }}"/><br>
            {% else %}
                <img border="0" alt="Profile Pic" src="/media/imagenes/nouserimg.png" height="140" width="140"/><br>
            {% endif %}

            Fecha de registro: <br> {{ mensaje.autor.date_joined|date:"M-d-Y" }} <br>
            Número de mensajes: {{ pdata.0 }}
        {% endwith %}
        </td>
        <td align="justify" class="celdacontenido" data-title='{{ mensaje.fecha|date:"m-d-Y, h:i a" }}'>
            {{ mensaje.contenido }}<br>
        </td>

        </tr>

        </tbody>
    </table>
</div>
<br>
<b>Desmarcando la casilla el mensaje se catalogará como inapropiado, caso contrario al seleccionar
    la casilla, será apropiado.</b> <br><br>

<form method="POST" action="">
    {% csrf_token %}
    Apropiado:
    {{ form.apropiado }}<br><br>
    <input class="btn btn-primary" type="submit" value="Confirmar" id="submit"/>
</form>

{% endblock %}

When I click on

<a href="{% url 'censura' mensaje.id %}" class="apropiado">Marcar como inapropiado</a>

from my other template, it works, I can see the 'censura' template without problems, but when I click on the submit button it gives the NoReverseMatch at /msjcensura/5/

Reverse for 'detalle_usuario' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['usuario/(?P<pk>\\d+)/']

at

{% url 'detalle_usuario' mensaje.autor.id %}

I don't understand this, I have specified other view ('mensajes'), but it goes to the same template (censura).

Edit, I had the url, I forgot to type it in the question, because the problem was in the redirect and the mensaje.autor.id returning 'None':

 url(r'^usuario/(?P<pk>\d+)/', LR(DetalleUsuario.as_view()), name="detalle_usuario"),

And the view:

class DetalleUsuario(DetailView):
model = PerfilUsuario
template_name = "foro/detalleUsuario.html"
silver1991
  • 25
  • 1
  • 10

2 Answers2

1

reverse() is used in function based views while reverse lazy() is used in class based views.

Example:

Calling the django reverse functionality from the get_success_url () method.

class MensajeApropiado(UpdateView):
    model = Mensaje
    form_class = FormMensaje
    template_name = 'foro/msjCensura.html'

    def get_success_url(self):
        return reverse('mensajes', args=(self.object.tema.id,))

Calling from success_url parameter.

class MensajeApropiado(UpdateView):
    model = Mensaje
    form_class = FormMensaje
    template_name = 'foro/msjCensura.html'
    success_url = reverse_lazy('mensajes', args=(self.object.tema.id,))

This is because class attributes are evaluated on import, reverse_lazy() handles the process of loading on import from within a class based view and reverse () from within a method of a class based view or a function based view. The answer to when or exactly how that happens, resides within the depths of python's import system.

See this answer also: Difference between reverse() and reverse_lazy() in Django

0

Try

{% url 'detalle_usuario' pk=mensaje.autor.id %}

This will help target the correct url since the parameter in your url config is named. Also, it seems like, mensaje.autor.id is None. Can you try and debug that mensaje.autor is present, and has an id?

zsquare
  • 9,916
  • 6
  • 53
  • 87