2 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
有幾種方法可以在不同的視圖(頁(yè)面)上存儲(chǔ)和顯示信息:
您可以使用會(huì)話:
#views.py
#set the session key in the view A:
request.session['info'] = {'key', 'value'}
#get the session key in the view B:
info = request.session['info']
del request.session['info']
#app/models.py
#model to store information from page A
from django.contrib.auth.models import User
class MyModel(models.Model):
user = models.ForeignKey(User,
on_delete=models.DELETE,
related_name="info")
info = models.JSONField()
#views.py
#store data in the view A:
from app.models import MyModel
MyModel.objects.create(user=request.user, info={'key', 'value'})
#retrieve data in the view B:
info = MyModel.objects.get(user=request.user)
但實(shí)際上,一切都取決于您打算實(shí)現(xiàn)的邏輯。實(shí)際上還有其他方法,例如使用異步 Ajax、React、WebSockets...

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以將其作為 HTML 請(qǐng)求參數(shù)傳遞到 url 中,例如,在您的 pageA 模板中
<a href="{% url 'pageB' %}?some_filter={{ some_filter }}">to page B</a>
在 pageb 視圖中,您可以從請(qǐng)求對(duì)象中獲取過(guò)濾器
def page_b_view(request):
some_filter = request.GET.get('some_filter')
添加回答
舉報(bào)