1

I am trying to customize my admin form by modifying change_form.html. Here's what I have so far:

In my forms.py, I have:

class ProblemForm(forms.ModelForm):

slug = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'link'}))

topic = forms.ModelChoiceField(label = "Topic", queryset = Topic.objects.all())

questionToProblem = forms.CharField(label = "Question", widget = CustomAdminPageDown(attrs={'id' : 'mathInputForQuestion', 'onchange':'Preview.Update()'}))

solutionToProblem = forms.CharField(label = "Solution", widget = CustomAdminPageDown(attrs={'id' : 'mathInputForSolution'}))

class Meta:

model = Problem

fields = ('questionToProblem', 'solutionToProblem')

In my change_form.html, I have:

{% extends "admin/change_form.html" %}

{% block after_field_sets %}{{ block.super }}

<body>

<p>Preview is shown here:</p>

<div id="MathPreview" style="border:1px solid; padding: 3px; width:50%; height: 20%; margin-top:20px" class="format-paragraph"></div>

<div id="MathBuffer" style="border:1px solid; padding: 3px; width:50%; height: 20%; margin-top:20px;

visibility:hidden; position:absolute; top:0; left: 0"></div>

</body>

However, I want the to be under the questionToProblem form, so that when I look at the page, the div is right below the textarea. How would I do that?

WannaInternet
  • 327
  • 1
  • 6
  • 14

1 Answers1

1

You could instead write a custom widget that is a subclass of CustomAdminPageDown, and depending on what version of Django you're using there are a couple ways you'd extend to add your preview block.

Django widget override template

Additionally, you can associate the file containing the Preview.Update() javascript using the Media class to keep things tidy.

https://docs.djangoproject.com/en/dev/topics/forms/media/#topics-forms-media

jburte
  • 11
  • 1
  • It seems that most resources are about completely overriding the template, but what I need is just adding a couple HTML elements. Would I do that using render()? – WannaInternet Jan 27 '20 at 23:32
  • It would depend on the version of Django that you are using. Since 1.11 widgets are rendered in a template. Before this they were rendered in Python. https://docs.djangoproject.com/en/1.11/ref/forms/renderers/ Your template will look nearly the same as it does in your example, but will extend the current widget's template instead of the whole form's template. Then, the template will be used to render the widget. – jburte Jan 28 '20 at 17:02