Skip to content

Commit

Permalink
Use mark_safe to render input
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodiegoss committed Jan 7, 2025
1 parent c0acbe6 commit da99fbf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/core/forms.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
from django import forms
from django.utils.safestring import mark_safe

from .models import Exhibit


class UploadFileForm(forms.Form):
file = forms.ImageField(required=False)


class ListTextWidget(forms.TextInput):
def __init__(self, *args, **kwargs):
super(ListTextWidget, self).__init__(*args, **kwargs)
self._name = 'exhibit-list'
self._list = Exhibit.objects.all().order_by('name')
self.attrs.update({'list':f'list__{self._name}'})
self._name = "exhibit-list"
self._list = Exhibit.objects.all().order_by("name")
self.attrs.update({"list": f"list__{self._name}"})

def render(self, name, value, attrs=None, renderer=None):
text_html = super(ListTextWidget, self).render(name, value, attrs=attrs)
data_list = f'<datalist id="list__{self._name}">'
for item in self._list:
data_list += f'<option value="{item.slug}">{item.name}</option>'
data_list += '</datalist>'
return (text_html + data_list)
data_list += "</datalist>"
return mark_safe(text_html + data_list)


class ExhibitForm(forms.Form):
exhibit = forms.CharField(required=True)
exhibit = forms.CharField(required=True)

def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs):
super(ExhibitForm, self).__init__(*args, **kwargs)
self.fields['exhibit'].widget = ListTextWidget()
self.fields['exhibit'].label = ""
self.fields["exhibit"].widget = ListTextWidget()
self.fields["exhibit"].label = ""
2 changes: 1 addition & 1 deletion src/core/jinja2/core/exhibit_select.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h2>{{_("Choose the Exhibition you're seeing")}}</h2>
<form name="exhibit-form" action="{{url('exhibit_select')}}" method="post" autocomplete="off">
{{ csrf_input }}
{{form}}
{{ form }}
<input class="submit-btn" type="submit" value="{{ _('Submit') }}"/>
</form>
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/users/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging

from django.conf import settings
Expand All @@ -12,7 +11,7 @@
from django.contrib.auth.views import PasswordResetView
from django.contrib.messages.views import SuccessMessageMixin
from django.http import Http404, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.decorators.cache import cache_page
Expand Down

0 comments on commit da99fbf

Please sign in to comment.