本文整理汇总了Python中django.template.response.TemplateResponse.set_cookie方法的典型用法代码示例。如果您正苦于以下问题:Python TemplateResponse.set_cookie方法的具体用法?Python TemplateResponse.set_cookie怎么用?Python TemplateResponse.set_cookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.response.TemplateResponse
的用法示例。
在下文中一共展示了TemplateResponse.set_cookie方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from django.template.response import TemplateResponse [as 别名]
# 或者: from django.template.response.TemplateResponse import set_cookie [as 别名]
def post(self, *args, **kwargs):
# Get the chosen options ids.
post = self.request.POST.copy()
try:
del post["csrfmiddlewaretoken"]
except KeyError:
pass
choice_ids = map(lambda v: int(v), post.values())
# Create a new answer and add the voter and the choices.
voter = get_voter(self.request)
answer = Answer.objects.create(
gallup_id=self.kwargs["gallup_id"],
voter=voter,
client_identifier=get_client_identifier(self.request)
)
choices = Option.objects.filter(pk__in=choice_ids).all()
answer.choices.add(*choices)
context = self.get_context_data()
context["show_results"] = True
context["disabled"] = True
context["answered_options"] = choices
response = TemplateResponse(self.request, self.template_name, context)
response.set_cookie(Voter.VOTER_COOKIE, voter.voter_id)
return response
示例2: view_cart
# 需要导入模块: from django.template.response import TemplateResponse [as 别名]
# 或者: from django.template.response.TemplateResponse import set_cookie [as 别名]
def view_cart(request):
if 'cart_id' in request.COOKIES:
cart_id = request.COOKIES["cart_id"]
cart, created = Cart.objects.get_or_create(id=cart_id)
else:
if request.user.is_authenticated():
cart, object = Cart.objects.get_or_create(user=request.user)
else:
cart = Cart.objects.create(user=None)
if request.is_ajax():
ret_dict = {}
ret_dict['success'] = True
ret_dict['item_type_count'] = cart.cart_products.all().count()
from shopcart.serializer import serializer
# serialized_cart = serializer(cart,datetime_format='string',output_type='dict',many=True)
# 先不返回购物车中商品信息
serialized_cart = serializer(cart, datetime_format='string', output_type='dict', many=False)
# logger.debug(serialized_cart)
ret_dict['cart'] = serialized_cart
return JsonResponse(ret_dict)
else:
ctx = {}
ctx['system_para'] = get_system_parameters()
ctx['menu_products'] = get_menu_products()
ctx['page_name'] = 'My Cart'
if request.method == 'GET':
ctx['cart'] = cart
response = TemplateResponse(request, System_Config.get_template_name() + '/cart_detail.html', ctx)
response.set_cookie('cart_id', cart.id, max_age=3600 * 24 * 365)
return response
示例3: show_pad
# 需要导入模块: from django.template.response import TemplateResponse [as 别名]
# 或者: from django.template.response.TemplateResponse import set_cookie [as 别名]
def show_pad(request, group_name, pad_name):
# test if user is in group
if not request.user.groups.filter(name=group_name).exists():
return TemplateResponse(request, 'etherpad/forbidden.html', {
'group_name': group_name,
}, status=403)
ep = Etherpad()
try:
ep.create_session(request.user, group_name)
group_id = ep.get_group_id(group_name)
pad_url = '{0}/p/{1}${2}'.format(
settings.ETHERPAD_URL,
group_id,
pad_name)
cookie = ep.get_session_cookie(request.user)
except URLError:
return TemplateResponse(request, 'etherpad/server_error.html', {
}, status=500)
is_fullscreen = 'fullscreen' in request.GET
response = TemplateResponse(request, 'etherpad/pad.html', {
'pad_url': pad_url,
'group_name': group_name,
'pad_name': pad_name,
'fullscreen': is_fullscreen,
'base_template': 'base_raw.html' if is_fullscreen else 'base.html'
})
cookie_domain = '.' + request.get_host()
response.set_cookie('sessionID', cookie, domain=cookie_domain)
response['Access-Control-Allow-Origin'] = "https://ep.mafiasi.de"
return response
示例4: home
# 需要导入模块: from django.template.response import TemplateResponse [as 别名]
# 或者: from django.template.response.TemplateResponse import set_cookie [as 别名]
def home(request):
duoshuo_jwt_token = None
ctx = {
'settings': settings,
}
if request.user.is_authenticated():
ctx['profile'] = UserProfile.objects.get(user_id=request.user.id) #django 1.7没有get_profile
# 实现JWT登录,参看:http://dev.duoshuo.com/docs/501e6ce1cff715f71800000d
token = {
"short_name": settings.DUOSHUO_SHORT_NAME,
"user_key": request.user.id,
"name": request.user.username
}
ctx['duoshuo_jwt_token'] = duoshuo_jwt_token = jwt.encode(token, settings.DUOSHUO_SECRET)
response = TemplateResponse(request, 'home.html', ctx)
response.set_cookie('duoshuo_token', duoshuo_jwt_token)
return response