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

徘徊边缘 提交于 2019-12-08 11:06:39

问题


I have defined two functions in views.py to obtain first prova.html with certain elementimenu and then, after clicking over one of them, I obtain again the page with elementimenu and elementi associated to elementimenu with the same id i.e.:

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

And here the code that I like to get better:

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>
    ...

I had used this "trick" in show_detail view so that I can see elementimenu even after calling this function.

Is there a more elegant way to do this?


回答1:


Yes, you can use the single view. Add the default None value for the id (or pk) argument:

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

And then map both urls to this view:

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


来源:https://stackoverflow.com/questions/29036331/django-can-double-render-to-response-be-unified-into-only-one-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!