Django,反向懒惰给NoReverseMatch

时间:2015-11-23 01:26:01

标签: python django django-views

我正在尝试更新模型的一个字段,我已经覆盖了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&#39;模板没有问题,但是当我点击提交按钮时,它给出NoReverseMatch at / msjcensura / 5 /

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 %}

我不明白这一点,我已经指定了其他观点(&#39; mensajes&#39;),但它会转到相同的模板(censura)。

编辑,我有网址,我忘了在问题中输入,因为问题出在重定向中,而且menaje.autor.id返回&#39;无&#39;:

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

观点:

class DetalleUsuario(DetailView):
model = PerfilUsuario
template_name = "foro/detalleUsuario.html"

1 个答案:

答案 0 :(得分:0)

reverse()用于基于函数的视图,而反向lazy()用于基于类的视图。

示例:

从get_success_url()方法调用django反向功能。

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,))

通过success_url参数进行调用。

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

这是因为类属性是在导入时求值的,reverse_lazy()处理从基于类的视图内导入的加载过程,以及从基于类的视图或基于函数的视图的方法内进行反向加载的过程。何时或确切如何发生的答案在于python导入系统的深度。

另请参阅此答案:Difference between reverse() and reverse_lazy() in Django