Create the application shell

In this section, you’ll use the Watson Language Translation service to detect the language. This simple use case will show you how to get started with the watson-developer-cloud Python SDK.

    1. In your Django project, create a new application named watsonlanguage:
      projwatson > python startapp watsonlanguage
    2. Register the application in the file:# Application definitionINSTALLED_APPS = (


    1. Edit the project level file. A best practice is not to overload the project-level file routing table. Instead, application patterns should point to an application-level file, which allows applications to be reused across projects. Add in a URL pattern for your new watsonlanguage application:urlpatterns = [
      url(r'^wl/', include('watsonlanguage.urls', namespace="watsonlang")),
      url(r'^admin/', include(,
      The r'^wl' specifies the URL pattern, that is, URLs with \wl\ relate to the application for which the routing table can be found in include('watsonlanguage.urls').
      < Because the project file is now pointing to an application-level file, you need to create the application-level file.


    1. Inside the watsonlanguage application directory, create a new file.


    1. Edit that file to add patterns for your first application page:from django.conf.urls import include, url
      from .views import wl
      urlpatterns = [
      url(r’^lang$’, wl.index, name=’langhome’),

      When you open the web page /wl/lang, Django will look for an index function in the file views/ file.


    1. Create a views subdirectory. In the views directory, create an empty file and a file.


    1. Add the following code to the file:
      # -*- coding: utf-8 -*-from django.shortcuts import render

      from django import forms

      import logging

      logger = logging.getLogger(__name__)


      class Form_language(forms.Form):

        txtdata = forms.CharField(required=True, label="Text to Process", widget=forms.Textarea)


      def index(request):

        allinfo = {}

        form = None 

        if request.POST:

          form = Form_language(request.POST)

          if form.is_valid():

            data = form.cleaned_data['txtdata']

  "Text to be processed is %s " % data)

            lang = "TBD"

            allinfo['lang'] = lang


            allinfo['error'] = "Form is invalid"


           form = Form_language()


        allinfo['form'] = form

        return render(request, 'watson/wlindex.html', allinfo )

      This code creates a single field form and renders it by using the watson/wlindex.html template. When the data is posted back, for now, it says that the language is “TBD”. This is where you will add the Watson Language Detection shortly.


    1. Add the coding UTF-8 line. The coding is very important when dealing with languages other than English in Python.


    1. Create the wlindex.html template in the directory watsonlanguage/templates/Watson.

      Normally, you add in the bootstrap styles, but for now, let’s keep it simple.


  1. Add the following code into the wlindex.html template file:<html>  <head>

        <title>Watson Language Detection Starter</title>




        <form method="post" action="{% url "watsonlang:langhome"%}">

          {% csrf_token %}


               {{ form.as_table }}


           <div><input type="Submit" value="Process"/> </div>




           {% if lang %}

              Language Detected as {{ lang }}      

           {% endif %}



           {% if error %}

              Error - {{ error }}        

           {% endif %}




    The application should now work. Try it out. You should also be able to push it to Bluemix.