Django: "double" 'render_to_response' 可以统一成只有一个视图吗?

Django: can "double" 'render_to_response' be unified into only one view?

我在 views.py 中定义了两个函数,首先使用某些 elementtimenu 获得 prova.html 然后,单击其中之一后,我再次获得带有 elementtimenuelementielementtimenu 关联的页面相同的 ID 即:

another_app.model.py

...
class ElementiTab(models.Model):
    author = models.ForeignKey('auth.User', null=True, blank=False)
    des = models.CharField(max_length=30)
    x = models.FloatField()
    y = models.FloatField()
    res = models.FloatField(default=0)
    created_date = models.DateTimeField(default=timezone.now)
...

这里是我希望改进的代码:

views.py

from another_app.model import ElementiTab

def show_elementi(request):
        elementimenu = ElementiTab.objects.all()
        return render_to_response('homepage/prova.html',{'elementimenu': elementimenu, 'user.username': request}, context_instance = RequestContext(request))


def show_detail(request,id):
        elementimenu = ElementiTab.objects.all()
        detail = get_object_or_404(ElementiTab, pk=id)
        return render_to_response('homepage/prova.html',{'elementimenu': elementimenu, 'detail': detail, 'user.username': request}, context_instance = RequestContext(request))

urls.py

...
      url(r'^homepage/prova/$', views.show_elementi),
      url(r'^show_detail/(?P<id>\d+)/$', views.show_detail),
...

prova.html

...

    <div class="elementi">
    {% for elementi in elementimenu %}  
    <a href="/show_detail/{{elementi.id}}">{{elementi.des}}</a>
    {% endfor %}
    </div>


                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <tr class="info">
                                <td width="35%" align="center"> NOME</td>
                                <td width="35%" align="center"> DATA CREAZIONE </td>
                                <td width="30%" align="center"> AUTORE </td>
                                </tr>
                                {% if detail %}
                                    <div class="dettagli">
                                        <tr>

                                        <td>{{detail.des}}</td> 
                                        <td>{{detail.created_date}}</td>
                                        <td>{{detail.author}}</td>                          
                                        </tr>
                                {% endif %}  

                                    </div>

                            </table>
                        </div>
    ...

我在 show_detail 视图中使用了这个 "trick",这样即使在调用之后我也能看到 elementtimenu这个功能。

有更优雅的方法吗?

是的,您可以使用单一视图。为 id(或 pk)参数添加默认的 None 值:

def show_elementi(request, pk=None):
    elementimenu = ElementiTab.objects.all()
    detail = get_object_or_404(ElementiTab, pk=pk) if pk else None
    return render(request, 'homepage/prova.html',
                  {'elementimenu': elementimenu, 'detail': detail})

然后将两个 url 映射到此视图:

url(r'^homepage/prova/$', views.show_elementi),
url(r'^show_detail/(?P<pk>\d+)/$', views.show_elementi),