Ask Your Question

How to show a detail view and populate it with external data

asked 2015-01-20 14:24:44 -0500

eugene.goldberg gravatar image

I have a custom panel, which contains a data table. When a specific row is selected, and an Action Link button is clicked, I need to display a detail view, and populate it with data, which comes from a REST API call to an external resource. How is this done?


P.S. The desired experiance is very simmilar to clicking on the Instance link within "Instances" table, and being shown the detail view for the selected Instance.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2015-01-20 23:12:24 -0500

updated 2015-01-20 23:15:13 -0500

This isn't my realm of expertise, but this link below may be worth taking a look at if you haven't already. Excerpt from the link "Horizon provides a DataTable class which simplifies the vast majority of displaying data to an end-user. We’re just going to skim the surface here, but it has a tremendous number of capabilities." Good luck :)

edit flag offensive delete link more


Thank you Omar for your response! I did use that information as my initial source. My current goal is to be able to click on a row in the Data Table, and be redirected to a Detail view for the selected object. I've been looking into this all day, and will continue tomorrow.

eugene.goldberg gravatar imageeugene.goldberg ( 2015-01-20 23:38:39 -0500 )edit

answered 2015-01-25 09:03:59 -0500

eugene.goldberg gravatar image

I have struggled through this for a couple of days on my own, and here is what I had to do to accomplish that:

In my tables.oy file I have added a LinkAction:

class CreateNewWorkload(tables.LinkAction): name = "horizon:mydashboard:workloads_panel:create" verbose_name = _("Create New Workload") url = "create" classes = ("btn-launch", "ajax-modal")

The "create" url value point to the following entry in my file:

url(r'^create/$', views.CreateView.as_view(), name='create'),

The CreateView entry from the points to the following View in my file:

class CreateView(forms.ModalFormView): form_class = project_forms.CreateWorkloadForm template_name = 'create_workload.html' success_url = "http://localhost:9000/mydashboard/workloads_panel/"

The CreateWorkloadForm entry from this file, in turn, points to the following entry in my file:

class CreateWorkloadForm(forms.SelfHandlingForm): name = forms.CharField(max_length="255", label=_("Workload Name")) description = forms.CharField(widget=forms.Textarea, label=_("Description"), required=False) image_choices = [] images = forms.ChoiceField(label=_("Images"), choices=image_choices) flavor_choices = [] flavors = forms.ChoiceField(label=_("Flavors"), choices=flavor_choices)

def __init__(self, request, image_choices=image_choices, flavor_choices=flavor_choices,
             *args, **kwargs):
    super(CreateWorkloadForm, self).__init__(request, *args, **kwargs)

    images, self._more, self._prev = api.glance.image_list_detailed(
    flavors = api.nova.flavor_list(request, True)

    for image in images:
    if len(image_choices) > 1:
        image_choices.insert(0, ('', _("Select an Image")))
    self.fields['images'].choices = image_choices

    for flavor in flavors:
    if len(flavor_choices) > 1:
        flavor_choices.insert(0, ('', _("Select an Flavor")))
    self.fields['flavors'].choices = flavor_choices

def handle(self, request, data):
        message = _('Creating Workload'), message)
        # This is where we actually create our Workload
        workload_url = "http://localhost:8000/api/workload/"
        workload_name = request.POST.get("name")
        workload_description = request.POST.get("description")
        flavor_id = request.POST.get("flavors")
        image_id = request.POST.get("images")
        post_data = {"name": workload_name, "description": workload_description,
                     "image": image_id, "flavor": flavor_id}
        post_data = json.dumps(post_data)
        req = urllib2.Request(workload_url,post_data)

        req.add_header('Content-Type', 'application/json')
        resp = urllib2.urlopen(req, post_data)
        return True
    except Exception:
        redirect = reverse("horizon:mydashboard:workloads_panel:index")
                          _('Unable to create Workload.'),

It is in this particular file I go about retreiving my external data, and populating my detail (i.e. Update Form) with that data

And, finally, in my UpdateView (back in the file) I have this "template_name = 'create_workload.html'" entry, which points to create_workload.html, which, in turn, includes _create_workload.html:


{% extends 'base.html' %} {% load i18n %} {% block title %}{% trans "Create Workload" %}{% endblock %}

{% block page_header %} {% include "horizon/common/_page_header.html" with title=_("Create Workload") %} {% endblock page_header %}

{% block main %} {% include "_create_workload.html" %} {% endblock %}


{% extends "horizon/common/_modal_form.html" %} {% load i18n %} {% load url from future %}

{% block form_id %}create_workload_form{% endblock %} {% block form_action %}{% url 'horizon:mydashboard:workloads_panel:create' %}{% endblock %}

{% block modal_id %}create_workload_modal{% endblock %} {% block modal-header %}{% trans "Create Workload" %}{% endblock %}

{% block modal-body %}

<fieldset> {% include "horizon/common/_form_fields.html" %} </fieldset>

{% trans "Description:" %}

{% trans "Create a new Workload."%}

{% trans "A Workload includes OS type from Images and Size from Flavors"%}

{% endblock %}

{% block ... (more)

edit flag offensive delete link more

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower


Asked: 2015-01-20 14:24:44 -0500

Seen: 229 times

Last updated: Jan 25 '15