Julia
Julia

Reputation: 11

django-ckeditor: html template doesn't render my ckeditor content properly although I am using {{ content | safe }}

I am using the django-ckeditor plugin to implement a ckeditor field in my django website. I have an updateView where you can edit the "notes" attribute of a model called Server. The ckeditor widget is working perfectly here in my server_update_form.html:

<div class="flex flex-col items-center justify-center px-6 py-8 mx-auto md:h-screen lg:py-0">
    <form class="w-full" action="" method="post">
      {% csrf_token %}
      {{ form.media }}
      <table>
        <div class="relative z-0 w-full mb-5 group">
          <form class="max-w-sm mx-auto">
            <label for="notes" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{{ form.notes.label_tag }}</label>
            {{ form.notes }}
          </form>
        </div>

      </table>

      <button 
        type="submit" 
        style="display:block" 
        class="w-1/10 text-white bg-orange-600 hover:bg-orange-700 focus:ring-4 focus:outline-none focus:ring-orange-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">
        Submit
      </button>
    </form>
  </div>

This is how it looks like

I also have a DetailView where I would like to display the content inside server.notes without being able to edit it. My server_detail.html looks like this:

{% if server %}
  <h1 class="mb-8 text-3xl font-bold">Server: {{ server.displayname }}</h1>

    </div>
    <div class="flex flex-col pt-3">
      <dt class="mb-1 text-gray-500 md:text-sm dark:text-gray-40">Notes</dt>
      <dd class="text-md mb-5 p-2 border rounded-md">
        {{ server.notes |safe  }}
      </dd>
    </div>
  </dl>

But html doesn't render the notes properly: DetailView page

model.py:

class Server(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text="Unique ID for this server",  editable=False)
    ip_address = models.CharField(max_length=30, help_text="Enter servers IP address")
    displayname = models.CharField(max_length=100, help_text="Enter name of server")
    description = models.CharField(max_length=1000, help_text="Enter short description of your server here", blank=True, null=True)
    model = models.CharField(max_length=100, help_text="Enter model", blank=True, null=True)
    notes = RichTextUploadingField(blank=True, null=True)    
    location = models.CharField(max_length=100, help_text="Enter servers location", blank=True, null=True)
    contabo_api = models.CharField(max_length=100, help_text="Enter contabo API", blank=True, null=True)
    nags_api = models.CharField(max_length=100, blank=True, null=True)
    hosting_provider = models.CharField(max_length=100, blank=True, null=True)
    production = models.BooleanField()

    class Meta:
        ordering = ['displayname']

    def __str__(self):
        return f'{self.displayname} ({self.ip_address})'
    
    def get_absolute_url(self):
        return reverse('server-detail', args=[str(self.id)])
    
    def no_applications(self):
        """
        returns the number of applications which are linked to this server.
        """
        return Application.objects.filter(server=self).count()

View.py:


class ServerDetailView(LoginRequiredMixin, generic.DetailView, ):
    model = Server
    context_object_name = 'server'

    def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
        """
        Extend context with applications of the server.
        """
        context = super().get_context_data(**kwargs)
        server_instance = self.get_object()

        applications = Application.objects.filter(server=server_instance.pk)
        context["applications"] = applications

        no_applications = Application.objects.filter(server=server_instance.pk).count()
        context["no_applications"] = no_applications

        return context

class ServerUpdate(LoginRequiredMixin, UpdateView):
    model = Server
    form = ServerForm
    template_name_suffix = '_update_form'
    fields = ['displayname', 'ip_address', 'description', 'model', 'location', 'hosting_provider', 'production', 'notes']

forms.py:

class ServerForm(ModelForm):
    content = CharField(widget=CKEditorUploadingWidget())
    class Meta:
        model = Server
        fields = ['notes']

I have wasted countless hours trying to fix this problem and I feel like I am going insane, I would appreciate it so much if anyone out there could offer his help!

Upvotes: 1

Views: 51

Answers (0)

Related Questions