I have an UpdateView which has extra context, ‘webrem’, added through a get_context_data() override. The problem that I have is that when I add a ‘|as_crispy_field’ tag to any of the extra context fields I get the above error.
I suspect that I need to give crispy some information about extra context fields, so that it can render them correctly, but I don’t know how. I have tried via ini but without success.
FYI the extra content is a one to one model with Webinar (the UpdateView model) as it’s primary key.
View
class WebinarUpdateView(UpdateView):
model = Webinar
form_class = WebinarAddForm
template_name = 'webinar/webinar-add-update.html'
extra_context = {'page_title': 'Edit Webinar'}
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.object:
context['webrem'] = WebinarReminder.objects.filter(webinar=self.object).first()
(the rest of the model omitted as is irrelevant)
Form
class WebinarAddForm(forms.ModelForm):
HOURS = "HR"
MINUTES = "MIN"
CHOICES_INTERVAL = [
(HOURS, 'hour(s) before'),
(MINUTES,'minute(s) before' ),
]
webinarpresenters = forms.ModelMultipleChoiceField(queryset=None)
notifications_unit_1 = forms.ChoiceField(widget=forms.Select(attrs={'class': 'btn-light notifications_unit custom-select'}),
label='',
choices=CHOICES_INTERVAL,
initial='MIN',
required=False)
notifications_interval_1 = forms.CharField(label='', max_length=2, required=False)
notifications_unit_2 = forms.ChoiceField(widget=forms.Select(attrs={'class': 'btn-light notifications_unit custom-select'}),
label='',
choices=CHOICES_INTERVAL,
initial='HR',
required=False)
notifications_interval_2 = forms.CharField(label='',
max_length=2,
required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["webinarpresenters"].queryset = WebinarPresenter.objects.filter(is_active=True)
class Meta:
model = Webinar
fields = ('title',
'description',
'webinar_event_link',
'webinarpresenters',
'notifications_unit_1',
'notifications_interval_1',
'notifications_unit_2',
'notifications_interval_2'
)
Models
class Webinar(models.Model):
title = models.CharField(max_length=100, blank=False, null=False)
description = models.TextField(blank=False, null=False)
webinar_event_link = models.URLField(default="https://zoom.com", blank=False, null=True)
webinarpresenters = models.ManyToManyField('WebinarPresenter', through='WebinarPresenting')
class WebinarReminder(models.Model):
notifications_unit_1 = models.CharField('', max_length=3, blank=True, null=True)
notifications_interval_1 = models.IntegerField(blank=True, null=True)
notifications_unit_2 = models.CharField('', max_length=3, blank=True, null=True)
notifications_interval_2 = models.IntegerField(blank=True, null=True)
notification_time_2 = models.DateTimeField(blank=True, null=True)
webinar = models.OneToOneField(Webinar, on_delete=models.CASCADE, primary_key=True)
The problem fields are:
‘notifications_unit_1’,
‘notifications_interval_1’,
‘notifications_unit_2’,
‘notifications_interval_2’
Traceback
[2023-06-25 08:02:44,995] ERROR: Internal Server Error: /webinar/webinar-add
Traceback (most recent call last):
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 204, in _get_response
response = response.render()
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/django/template/base.py", line 698, in resolve
new_obj = func(obj, *arg_vals)
File "/Users/pj/Documents/dev/BACKOFFICE/venv/lib/python3.9/site-packages/crispy_forms/templatetags/crispy_forms_filters.py", line 99, in as_crispy_field
raise CrispyError("|as_crispy_field got passed an invalid or inexistent field")