Insights from Our Experts

Blog image

Django - DHL Integration

Author Image

Sandeep Balagopal,Sr Software Engineer

An app to integrate DHL shipping module with Django based e-commerce projects.

Compatible with Django Oscar, Satchmo,LFS,django-shop

 

View on Github

 

Get Account and Toolkit

 

1. Get the toolkit from DHL website 

XML PI Info

XML PI Toolkit

2.Register with DHL

Register with DHL from here

You need to get the SiteID and Password to integrate DHL. Once the above steps are complete, you are ready to start the integration.

How DHL XML API Works

DHL API is running on J2EE Servlet. People who has only the Django background can think of J2EE servlet as Controller in Django. Whenever you give any URL in Django, it finally hit the controllers method and execute starts from there.
DHL has two servlets. One for testing and one for production. Here it is.

Production URL
Test URL
     2.  DHL servlet accepts only the XML as input and returns XML as Response. If you want to execute any steps, you need to create           the XML before access the XML Servlet.

What we do in this app 

The XML-PI Toolkit you downloaded from DHL has a "clientSoftware",which implements the XML requests in Java.We write these XML requests in Django.

Three types of requests are implemented.

Pickup service
Quote service
Shipment validation
Sample usage

from django.views.generic import TemplateView from dhl.modules.pickup import DHLXMLPickup from dhl.models import DHLTransaction from dhl.forms import PickUpForm

class PickupCreateView(TemplateView):
pickup_form = PickUpForm pickup_cancel_form = PickupCancelForm order = None template_name = 'pickup_request.html'

def get_context_data(self, **kwargs):
ctx = super(PickupCreateView, self).get_context_data(**kwargs) ctx['pickup_form'] = self.pickup_form() return ctx
def post(self, request, *args, **kwargs):
pickup_form = self.pickup_form(data=request.POST) order = self.order # order must be an order instance.You have to set order to current order which needs to be shipped. if pickup_form.is_valid():

pickup_date = pickup_form.cleaned_data.get('pickup_date') ready_time = pickup_form.cleaned_data.get('ready_time').strftime("%H:%M") close_time = pickup_form.cleaned_data.get('close_time').strftime("%H:%M") dhl_pick = DHLXMLPickup(order,params={'PickupDate':pickup_date,'ReadyByTime':ready_time,'CloseTime':close_time}) dhl_pick.trigger_pickup_dhl() error = dhl_pick.get_dhl_error() if dhl_pick.get_dhl_error() else "" if not error:

try:
dhl_transation = DHLTransaction.objects.create(
order=order, type='pickup', xml_response = dhl_pick.response, )
except Exception as e:
print "exception is",e
cxt = self.get_context_data() cxt.update({'pickup_form':pickup_form}) return self.render_to_response(cxt)