Tôi có dự án bình python này và tôi yêu cầu logic sẽ tạo cookie nếu cookie không tồn tại.bình python không tạo cookie khi hết hạn
Khi cookie được tạo, tôi cần lưu trữ cookie, trong số các giá trị khác, vào cơ sở dữ liệu cho mục đích theo dõi.
Đây là một phần của mã hiện tại của tôi:
import uuid
from flask import render_template, request, make_response
def load_cookie_config(context):
#context is a dictionary that is passed
key = 'some_key'
template_name = 'some_template'
cookie = request.cookies.get(key, None)
if not cookie:
guid = str(uuid.uuid4())
context['cookie_quid'] = guid
rsp = make_response(render_template(template_name, **context))
rsp.set_cookie(key, guid)
#... some method call insert values into db
else:
result = '' #.. some method call to get values from db
context['cookie_quid'] = cookie
rsp = make_response(render_template(template_name, **context))
return rsp
Đoạn mã trên công trình và nó tạo ra cookie như mong đợi, tuy nhiên nó mặc định cookie hết hạn bất cứ khi nào phiên trình duyệt kết thúc.
tôi cần phải thiết lập các cookie cho nơi mà nó sẽ hết hạn sau một số tiền X ngày so với phiên trình duyệt, nhưng khi tôi thay đổi dòng này:
rsp.set_cookie(key, guid)
để
rsp.set_cookie(key, guid, expires=90)
Nó không tạo cookie.
Có ai có suy nghĩ hoặc ý tưởng về lý do tại sao điều này có thể là?
này làm việc thật đẹp, cảm ơn bạn rất nhiều vì đã phản ứng nhanh chóng! – Wondercricket
Tất nhiên :) Tôi rất vui vì tôi đã có thể giúp đỡ – CodeLikeBeaker
Độ tuổi tối đa có dấu gạch dưới, ví dụ: response.set_cookie (khóa, guid, max_age = 90) – reggoodwin