Tuesday 30 December 2014

How to authenticate or login in django ?

# Authenticate or login in django

1. Open views.py

2. Import this line in views.py .
    from django.contrib.auth import authenticate, login

3. Create function
eg. 
 class Auth(View):
    def post(self, request, *args, **kwargs):
        use = request.POST['username']
        passw = request.POST['password']
        user = authenticate(username=use, password=passw)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponse(login)
                        # Redirect to a success page.
            else:
                return HttpResponse("disable")
          
                        # Return a 'disabled account' error message
        return HttpResponse("Invalid username or password ")
                # Return an 'invalid login' error message."

How to split the URL or String in django python ?

# Split the URL or String in django

1. You can split the string or url with the help of split function

eg.
     url = '127.0.0.1:8000/serverpage/forgot/4'
     stringsplit = url.split('/');
     filter = stringsplit[3]
     print(filter)
    
     Output is: 4

How to get current url path in Django python?

# Get current url path in Django

1. Use this code

    currenturl = request.get_full_path()

    print(currenturl)

* If you will use  print () then output will be shown in your terminal

How to use encode or decode in django python ?

# Encode or decode in django

1. Open views.py
     import base64
     import urllib


2. Use base64 in function for encode or decode

    encode = base64.b64encode(str('2'))
    print(encode)
    output: Mg==

    decode  = base64.b64decode('Mg==')
    url=urllib.unquote(decode).decode('utf8') -----optional
    print(url)
    output: 2
   

How to Send Email in django ?

# Send Email in django

1. Open setting.py in your Project.

2. Configure email settings in setting.py 
 
   EMAIL_HOST = 'mail.example.com'
   EMAIL_HOST_USER = 'Enter your email '
   EMAIL_HOST_PASSWORD = ' Enter your email password'
   EMAIL_PORT = port number
   EMAIL_USE_TLS = False

3. Open views.py
    import from django.core.mail import send_mail

4. Create function
    eg. def sendemail(request):
                send_mail('Subject', Message, 'Sender email', [ToEmail], fail_silently=False)


How to import large size database in mysql ?

 # Import large size database in mysql

1. Open terminal or cmd ?

2. Run this command on terminal or cmd

      mysql -u (username) -p database_name < file.sql

eg. mysql -u root -p testing </home/ghrix/nick/test.sql

How to connect mysql in Django python

# Connect mysqlDB in Django python ?

1. Open the settings.py in your project.

2.  Replace this code in setting.py

DATABASES = {

    'default': {
        'NAME': 'Enter your db name',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'Enter mysql username',
        'PASSWORD': 'Enter mysql password',

    }
}