【发布时间】:2016-02-25 01:13:06
【问题描述】:
我正在尝试更新模型的一个字段,我已经覆盖了 updateview 中的 get_success_url 方法,但它不起作用。
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"),
这是模板:
{% 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 %}
当我点击时
<a href="{% url 'censura' mensaje.id %}" class="apropiado">Marcar como inapropiado</a>
从我的其他模板中,它可以正常工作,我可以毫无问题地看到“censura”模板,但是当我单击提交按钮时,它会在 /msjcensura/5/ 处提供 NoReverseMatch
Reverse for 'detalle_usuario' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['usuario/(?P<pk>\\d+)/']
在
{% url 'detalle_usuario' mensaje.autor.id %}
我不明白这一点,我指定了其他视图('mensajes'),但它转到相同的模板(censura)。
编辑,我有网址,我忘了在问题中输入它,因为问题出在重定向和 mensaje.autor.id 返回“无”:
url(r'^usuario/(?P<pk>\d+)/', LR(DetalleUsuario.as_view()), name="detalle_usuario"),
还有观点:
class DetalleUsuario(DetailView):
model = PerfilUsuario
template_name = "foro/detalleUsuario.html"
【问题讨论】:
-
mensaje.autor或mensaje.autor.id均为无。如果你打印出mensaje.autor,你会得到什么? -
我已经更改了代码,我正在使用两个视图: MensajeCensura(request, **kwargs) 来呈现模板和 ConfirmarCensura(request) 来更新字段,现在它可以工作了,我知道这是不是问题的解决方案,但我需要这个项目在星期二工作
-
你没有
detalle_usuariourl -
您好,我已添加该信息
标签: python django django-views