代码之家  ›  专栏  ›  技术社区  ›  skimobear

Django让所有提交按钮返回主页

  •  0
  • skimobear  · 技术社区  · 7 年前

    我有一个应用程序,有一个主页,有几个按钮。每个按钮执行一些查看功能,然后返回主页。单击下面的按钮时,返回主页,但url显示: http://localhost:8080/abc_app/backup_system

    还有什么需要我回去的吗 http://localhost:8080/abc_app http://localhost:8080/abc_app/backup_system

    提前谢谢。

    <form action="{% url 'abc_app:backup_system' %}" method="post">{% csrf_token %}
       <button type="submit" name="index" class="btn btn-primary btn-block float-right"value="index">Refresh</button>
    </form>
    def backup_system(request):
        status = g_abc_api.backup_system()
        return index(request)
        # I have tried HttpResponseRedirect('/compost') as well.
        # If possible, it would be great to have the 'status' variable returned from the API available in  index(request) so I can display errors.
    
    def index(request):
        '''
        Populate context
        ''
        return render(request, 'abc_app/index.html', context)
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Natiq Vahabov    7 年前

    您可以使用HttpResponseRedirect

    from django.http import HttpResponseRedirect
    ...
    return HttpResponseRedirect('your_template_name')