【发布时间】:2021-02-27 10:38:44
【问题描述】:
对于我的 Python/Djang 应用,我希望在每个页面级别都有“返回”按钮,这应该会带您进入您来自的页面。
我的结构:
Main webpage
-> All patients list -> View patients credentials -> Edit patients data
-> Dentists patients list -> View patients credentials -> Edit patients data
-> Orthopedy patients list -> View patients credentials -> Edit patients data
第一个问题是,可以从不同的页面访问“查看患者凭据”。 我在 Rahul Gupta Returning to page that brought you there Django 的帮助下通过了它,并使用了 url:
'?next={{request.path}}'
当我想使用时出现问题 通过“查看患者凭据”从“编辑患者数据”返回 2 次的相同解决方案 到把你带到那里的网络。 有没有更好的方法来处理这个问题,而不是每一步都添加新的“下一个”地址:即。 '?next1=XX?next2=YY?next3=ZZ' 等等? 只需两个步骤,这个解决方案就可以了,但是如果我有更多的步骤,或者如果 我需要按钮才能像网页中的“后退按钮”一样工作,例如引导您浏览 10 个或更多最后一页。
我的代码:
在“所有患者列表”、“牙医患者列表”、“骨科患者列表”中,我使用:
{% for patient in patient_list %}
<a href='{% url 'patients:patientscredentials' pk=patient.pk %}?next={{request.path}}'>
{{ patient }}
</a>
{% endfor %}
在我使用的 PatientCredentialsView 中:
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['next_url'] = self.request.GET.get('next')
return context
在我的“patients_credentials.html”中,我使用:
<a class='btn btn-primary' href={{ next_url }}>Back</a>
非常感谢您的帮助!
【问题讨论】:
-
你可以简单地使用javascript和
window.history.back();,不需要用django做太多