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"