Python Django

Django connecting with mysql database

step 1

we need to install mysql module

pip install PyMySQL

step 2

configure mysql in


# Database

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'pypaywheel',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
        'OPTIONS': {'charset': 'utf8mb4'},


    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoblog',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
        'OPTIONS': {'charset': 'utf8mb4'},
By bm on July 8, 2014 | Python Django | A comment?

Django createing common static folder



{% load staticfiles %}

   <title>{% block title %}My amazing site{% endblock %}</title>
   <link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" type="text/css">
   <script src="{{ STATIC_URL }}js/jquery-2.1.1.min.js"></script>
By bm on | Python Django | A comment?

Django accessing request object in template

#for accessing request objext in templet

#default settings 

#custome settings for loding request in templet


{{ request.user.first_name }} {{request.user.last_name}}


{% if request.user.username %}

Hi {{ request.user.first_name }} {{request.user.last_name}} (<a href="/logout">Logout</a>)

{% else %}
<a href="/login">Login</a> 

{% endif %}

By bm on | Python Django | 1 comment

Django Log configuration in

    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
    'handlers': {
        'null': {
        'logfile': {
            'filename':  BASE_DIR+"/debug.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
            'formatter': 'standard'
    'loggers': {
        'django': {
            'propagate': True,
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,

        'appName': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',



import logging

# Get an instance of a logger
log = logging.getLogger(__name__)

    log.debug("Hey there it works!!")"Hey there it works!!")
    log.warn("Hey there it works!!")
    log.error("Hey there it works!!")

By bm on | Python Django | A comment?

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="" will set a cookie that is readable by the domains, and 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


deleting cookies 

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

By bm on | 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?