Skip to content

Instantly share code, notes, and snippets.

@eugene-goldberg
eugene-goldberg / gist:03d272fb41f56931b24e
Created January 21, 2015 17:04
horizon - forms - issue with form_action:
The issue is: when the form_action in my _create.html is specified this way: {% block form_action %}{% url 'workloads_panel:create' %}{% endblock %} - I get NoReverseMatch: u'workloads_panel' is not a registered namespace; "GET /mydashboard/workloads_panel/create/ HTTP/1.1" 500 35672
Here is my directory structure under openstack_dashboard/dashboards/mydashboard:
.
├── blueprints_panel
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── panel.py
tables.py:
class UpdateWorkload(tables.LinkAction):
name = "update"
verbose_name = _("Edit Workload")
url = "horizon:mydashboard:workload_panel:update"
classes = ("ajax-modal",)
icon = "pencil"
class UpdateView(forms.ModalFormView):
form_class = project_forms.UpdateWorkload
template_name = 'update_workload.html'
context_object_name = 'workload'
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super(UpdateView, self).get_context_data(**kwargs)
context["id"] = self.kwargs['id']
return context
class UpdateWorkload(tables.LinkAction):
name = "update"
verbose_name = _("Edit Workload")
url = "horizon:mydashboard:workloads_panel:update"
classes = ("ajax-modal",)
icon = "pencil"
class WorkloadsTable(tables.DataTable):
name = tables.Column("name", verbose_name=_("Name"))
views.py:
class UpdateView(forms.ModalFormView):
form_class = project_forms.UpdateWorkload
template_name = 'update_workload.html'
context_object_name = 'workload'
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super(UpdateView, self).get_context_data(**kwargs)
ERRORS:
Internal Server Error: /mydashboard/workloads_panel/3/update
Traceback (most recent call last):
File "/home/eugene/dev/horizon/.venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/home/eugene/dev/horizon/.venv/local/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/home/eugene/dev/horizon/.venv/local/lib/python2.7/site-packages/django/template/response.py", line 82, in rendered_content
content = template.render(context)
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% load url from future %}
{% block form_id %}update_workload_form{% endblock %}
{% block form_action %}{% url 'horizon:mydashboard:workloads_panel:update' %}{% endblock %}
{% block modal_id %}update_workload_modal{% endblock %}
{% block modal-header %}{% trans "Update Workload" %}{% endblock %}
@eugene-goldberg
eugene-goldberg / gist:b28c5bd260c97191bd26
Created January 23, 2015 16:18
ChoiceField - dynamic attribute value assignment
I need to assign the value to the 'initial' attributes for the images and flavors fields.
Here is how I do it currently.
No errors, the print shows that assignment took, but in the actual html form, I still have my default choice list, with no item selected, as desired
forms.py:
class UpdateWorkload(forms.SelfHandlingForm):
name = forms.CharField(max_length="255", label=_("Workload Name"))
description = forms.CharField(widget=forms.Textarea,
label=_("Description"), required=False)
resources:
class BlueprintResource(ModelResource):
# workloads = ManyToManyField(Workload, 'workloads')
workloads = fields.ManyToManyField('catalog.api.WorkloadResource', attribute='workloads',
full=True, null=True)
def obj_create(self, bundle, request=None, **kwargs):
return super(BlueprintResource, self).obj_create(bundle, request)
models:
class Blueprint(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=120)
description = models.TextField()
# workloads = models.ManyToManyField('Workload', db_constraint=False)
# workloads = models.ManyToManyField('self', null=True)
class Meta: