Python

Django cookies

setting cookies


    from django.shortcuts import render
    
    response = HttpResponse()
    response = render(request, 'hr/index.html',{'leaveRequests': leaveRequests,})
    response.set_cookie(key='sampCookieKey', value=1)
    return response  


syx:-  set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False)

max_age should be a number of seconds, or None (default) if the cookie should last only as long as the client’s browser session. If expires is not specified, it will be calculated.

expires should either be a string in the format "Wdy, DD-Mon-YY HH:MM:SS GMT" or a datetime.datetime object in UTC. If expires is a datetime object, the max_age will be calculated.

Use domain if you want to set a cross-domain cookie. For example, domain=".lawrence.com" will set a cookie that is readable by the domains www.lawrence.com, blogs.lawrence.com and calendars.lawrence.com. Otherwise, a cookie will only be readable by the domain that set it.

Use httponly=True if you want to prevent client-side JavaScript from having access to the cookie.


======================================================

Signed cookie
set_signed_cookie(key, value, salt='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=True


=========================================================

getiing cookies value

request.COOKIES.get('sampCookieKey')

==========================================================
deleting cookies 

request.delete_cookie(key, path='/', domain=None)


By bm on July 8, 2014 | Python Django | A comment?

Django session

settings Session

request.session['foo'] = 'bar'



retrieving value from session

x = request.session['foo']



deleting session

del request.session['foo']

By bm on | Python Django | A comment?

Django model queries

Django model (sql) queries

This is a dump data

posts =leaveRegister.objects.filter(status='') #.values()



----------

geting releted joins

eg:-tables : User, leaveRegister, leaveType
    bs = leaveRegister.objects.all().select_related() #leaveType
    for b in bs:
       
        log.info(b.leaveType.status)
        log.info(b.hrId.username)
        log.info(b.uId.username)
        log.info("----------------------")


------------------dump------
    
    #posts = leaveRegister.objects.order_by('-pub_date')[:5]
    
    #cheese_blog = Blog.objects.get(name="Cheddar Talk")
    
    #posts = leaveRegister.objects.filter(status='' )
    
    #posts = leaveRegister.objects.get(status='' )
    
    posts =leaveRegister.objects.filter(status='') #.values()
    
    #claimed_opponents = User.objects.filter(gameclaim_opponent__me__user=claimer)
    
    #posts=leaveRegister.objects.all().select_related('leaveType')



-----------------------------------

get user by id

u = User.objects.get(request.POST['profile_id'])
By bm on | Python Django | A comment?

Django how to get form values in views

    subject = request.POST.get('subject', '')
    message = request.POST.get('message', '')
    from_email = request.POST.get('from_email', '')
By bm on | Python Django | A comment?

Django send Email


from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', '[email protected]',    ['[email protected]'], fail_silently=False)

========================

email = EmailMessage('Hello', 'Body goes here', '[email protected]',
            ['[email protected]', '[email protected]'], ['[email protected]'],
            headers = {'Reply-To': '[email protected]'})

===============

Html email

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()


---
email.txt:

Hello {{ username }} - your account is activated.

email.html:

Hello <strong>{{ username }}</strong> - your account is activated.

--



https://docs.djangoproject.com/en/dev/topics/email/
By bm on | Python Django | A comment?

Django generating response output

Methord 1

    from django.http import HttpResponse
    
    from django.template import RequestContext, loader


    template = loader.get_template('hr/index.html')
    
    context = RequestContext(request, {'leaveRequests': leaveRequests,})  
    
    return HttpResponse(template.render(context))


Methord 2


    from django.shortcuts import render
    from django.http import HttpResponse


    response = HttpResponse()
    response = render(request, 'hr/index.html',{'leaveRequests': leaveRequests,})
    response.set_cookie(key='sampCookieKey', value=1)
    return response  

By bm on | Python Django | A comment?