4
votes

I'm a bit new to web development and am having trouble understanding how site content is stored. Even after looking at open source projects, I am still confused.

Lets say that I have a web page with multiple paragraphs that I am hoping to eventually be able to edit through the Django admin page. Now, I have the view set up for the page but I am unsure as how the models should be structured.

So far I have:

from django.db import models

class content(models.Model):
    name = models.CharField(max_length=30)
    last_updated = models.DateField(blank=True,null=True)
    content = models.TextField()

'name' is what my paragraphs will be talking about and 'content' should be all the paragraphs on that page. My question: Should the content field be stored in HTML or should I just store purely text? If it is pure text, how will I be able to add the HTML formatting when I embed the model.content field into my template? Wouldn't this be difficult see as I would need to parse the pure text to do so?

However, if I store HTML in the content field, wouldn't I have to use HTML when entering content or use a WYSIWYG editor in the admin page?

Thank you for any help :)

2

2 Answers

2
votes

In admin, you can use a WYSIWIG editor like TinyMCE This would store the content as HTML. While rendering - you can do {{content|safe}}.

Look into django-flatpages. it does exactly what you are shooting for.

1
votes

Look into django-tinymce. Its a good package which provides a widget to be associated with a CharField in forms for front-end and a model field to displayed as a WYSIWYG editor in admin.

And yes it will store the text content as html in database.