Wednesday, December 13, 2017

Re: Django field model for HTML parser?

On 14/12/2017 4:00 PM, drone4four wrote:
> Hi fellow Djangoistas:
>
> I'm back.
>
> Thank you Atonis for your help.  I suppose I can leave the WYSIWYG
> editor static files for later when I have alittle more experience. 
> For now though: how can I enable HTML markup to parse like it should? 
> The box where users on my Django website make a blog post, they can
> attach images, apply a timestamp but the content is just plain text. 
> How do I make it so that when I user for example enters this:
>
> |
> <h1>Lorem Ipsum</h1>
> <strong>Hello and welcome to my first blog post</strong>
> |
>
> ...that it posts and parses not as plain text but with the heading
> showing with a slightly larger font size than the rest and the first
> sentence is bolded?

That is easy but dangerous. You need to permit only a subset of markup.
In your views you need to bleach [1] the user entered data then
mark_safe() it. Bleach has a default set of html tags probably including
<h1> and <strong> but you can add others. For all other tags it will
convert angle brackets into &gt; and &lt; thus disabling them. Django's
mark_safe will deliver any real tags to the browser.

[1] https://pypi.python.org/pypi/bleach



>
> Thanks again.
>
> -Drone4four
>
> On Sunday, November 19, 2017 at 5:31:37 AM UTC-5, Antonis Christofides
> wrote:
>
> Hello,
>
> First of all: If you are just starting to learn, and because the
> amount you have to learn can be overwhelming, I'd suggest to not
> care about the wysiwyg editor at this stage. Pretend that your
> users can enter HTML in the field, and do all the rest. After you
> get some understanding of static files, only then come back to
> this issue.
>
> But to (prematurely) answer your question, if you have DEBUG=True
> and running django with manage.py runserver, you shouldn't need to
> care about STATIC_ROOT and STATIC_URL and collectstatic. Just
> forget about them. When the time comes to deploy your application
> and turn DEBUG off, then you will need to understand how static
> files work in production
> <https://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/>.
>
> Regards,
>
> Antonis
>
> Antonis Christofides
> http://djangodeployment.com
>
>
> On 2017-11-19 01:38, drone4four wrote:
>>
>> The official Django docs specify that STATIC_ROOT is indicated in
>> settings.py at the STATIC_URL variable (which is near the
>> bottom).  The official Django docs says:
>>
>> Configure your web server to serve the files in STATIC_ROOT
>> <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-STATIC_ROOT>under
>> the URL STATIC_URL
>> <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-STATIC_URL>.
>>
>>
>> (link
>> <https://docs.djangoproject.com/en/1.11/howto/static-files/deployment/>)
>>
>>
>> I'm not running Apache. My Django serving is running locally. The
>> absolute path in my file tree to my Django root directory is:
>>
>> /home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/dobbs_portal_blog/
>>
>> Inside dobbs_portal_blog is where I placed the static folder.
>> Therefore, the line in my settings.py where I declare the
>> variable, I changed from the default:
>>
>> |
>> *STATIC_URL ='/static/'*
>> |
>>
>> to:
>>
>> *|
>> STATIC_URL
>> ='/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/dobbs_portal_blog/static/'
>> |*
>>
>> That is my best guess at attempting resolve the "pretty obvious"
>> error message:
>>
>> You're using the staticfiles app without having set the
>> STATIC_ROOT setting to a filesystem path.
>>
>>
>> Yet I am still doing something wrong because invoking python3
>> dobbs_portal_blog/manage.py collectstatic, I still get the same
>> message:
>>
>>
>> $ python3 dobbs_portal_blog/manage.py collectstatic
>> You have requested to collect static files at the destination
>> location as specified in your settings.
>> This will overwrite existing files! Are you sure you want to
>> do this?
>> Type 'yes' to continue, or 'no' to cancel: yes Traceback
>> (most recent call last):  File "dobbs_portal_blog/manage.py",
>> line 22, in <module>    execute_from_command_line(sys.argv)
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/__init__.py",
>> line 364, in execute_from_command_line    utility.execute()
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/__init__.py",
>> line 356, in execute
>>    self.fetch_command(subcommand).run_from_argv(self.argv)
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/base.py",
>> line 283, in run_from_argv    self.execute(*args,
>> **cmd_options)  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/base.py",
>> line 330, in execute    output = self.handle(*args,
>> **options)  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 199, in handle    collected = self.collect()  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 124, in collect    handler(path, prefixed_path, storage)
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 354, in copy_file    if not self.delete_file(path,
>> prefixed_path, source_storage):  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 260, in delete_file    if
>> self.storage.exists(prefixed_path):  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/files/storage.py",
>> line 392, in exists    return os.path.exists(self.path(name))
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py",
>> line 50, in path    raise ImproperlyConfigured("You're using
>> the staticfiles app "
>> django.core.exceptions.ImproperlyConfigured: You're using the
>> staticfiles app without having set the STATIC_ROOT setting to
>> a filesystem path.
>>
>> Thanks for your attention and thank you Simon for your patience.
>>
>>
>> On Saturday, November 18, 2017 at 3:54:11 PM UTC-5, Simon Connah
>> wrote:
>>
>> The error message is pretty obvious.
>>
>> "You're using the staticfiles app without having set the
>> STATIC_ROOT setting to a filesystem path."
>> Set the STATIC_ROOT setting in settings.py.
>> https://docs.djangoproject.com/en/1.11/ref/settings/#static-root
>> <https://docs.djangoproject.com/en/1.11/ref/settings/#static-root>
>>
>>
>> On Saturday, 18 November 2017, 20:12:52 GMT, drone4four
>> <drone...@gmail.com> wrote:
>>
>>
>> Thank you, Jason.  The WYSIWYG editor like ckeditor is
>> precisely what I am looking for.
>>
>>
>> I have set out to run ckeditor.  I am following along with
>> the instructions on how to install it
>> <https://django-ckeditor.readthedocs.io/en/latest/#installation>.
>>
>>
>> Inside my virtual environment I invoke pip install
>> django-ckeditor.
>>
>>
>> Then I add ckeditor to INSTALLED_APPS in settings.py.
>>
>>
>> But then my shell is telling me that I have improperly
>> configured static root in the files app even though my
>> settings.py does include a line: STATIC_URL = '/static/'
>>
>> The installation doc linked to above does refer to the
>> official Django doc on how to manage static files
>> <https://docs.djangoproject.com/en/dev/howto/static-files/>,
>> which I find to be helpful but isn't really very applicable
>> to my particular situation because I do not have any image
>> files that I am working with in my case at this point.
>>
>>
>> When I run python3 dobbs_portal_blog/manage.py collectstatic,
>> I get this error:
>>
>>
>> $ python3 dobbs_portal_blog/manage.py collectstatic
>>
>>
>> You have requested to collect static files at the destination
>>
>> location as specified in your settings.
>>
>>
>> This will overwrite existing files!
>>
>> Are you sure you want to do this?
>>
>>
>> Type 'yes' to continue, or 'no' to cancel: yes
>>
>> Traceback (most recent call last):
>>
>>  File "dobbs_portal_blog/manage.py", line 22, in <module>
>>
>>    execute_from_command_line(sys.argv)
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/__init__.py",
>> line 364, in execute_from_command_line
>>
>>    utility.execute()
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/__init__.py",
>> line 356, in execute
>>
>>    self.fetch_command(subcommand).run_from_argv(self.argv)
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/base.py",
>> line 283, in run_from_argv
>>
>>    self.execute(*args, **cmd_options)
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/management/base.py",
>> line 330, in execute
>>
>>    output = self.handle(*args, **options)
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 199, in handle
>>
>>    collected = self.collect()
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 124, in collect
>>
>>    handler(path, prefixed_path, storage)
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 354, in copy_file
>>
>>    if not self.delete_file(path, prefixed_path, source_storage):
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",
>> line 260, in delete_file
>>
>>    if self.storage.exists(prefixed_path):
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/core/files/storage.py",
>> line 392, in exists
>>
>>    return os.path.exists(self.path(name))
>>
>>  File
>> "/home/gnull/Dropbox/TECH/python/2017/django-experiment-with-nick/subgenius-blog-env/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py",
>> line 50, in path
>>
>>    raise ImproperlyConfigured("You're using the staticfiles app "
>>
>> django.core.exceptions.ImproperlyConfigured: You're using the
>> staticfiles app without having set the STATIC_ROOT setting to
>> a filesystem path.
>>
>> (subgenius-blog-env) gnull at gnosis in
>>
>> $
>>
>>
>> On Saturday, November 18, 2017 at 7:46:21 AM UTC-5, Jason wrote:
>>
>> What you're trying to look for is a WYSIWYG editor (What
>> You See Is What You Get), and there are a number of third
>> party packages you can look at
>> https://djangopackages.org/ grids/g/wysiwyg/
>> <https://djangopackages.org/grids/g/wysiwyg/>
>>
>> ckeditor is one of the largest and most popular such
>> projects, and has integration with django via
>> https://github.com/django- ckeditor/django-ckeditor
>> <https://github.com/django-ckeditor/django-ckeditor>
>>
>>
>> On Friday, November 17, 2017 at 9:21:57 PM UTC-5,
>> drone4four wrote:
>>
>> I didn't do a very good job explaining.  Let me try
>> again.
>>
>>
>> If you take a look at this image of Blogger, the red
>> arrow points to the formatting bar
>> <https://imgur.com/LpksoNE>.  The formatting is a
>> helpful feature in Blogger which allows blog posters
>> to add an essay worth of content, and then alter the
>> appearance of the Lorem Ipsum essay content with
>> bold, italics, underline and a few dozen other
>> buttons and options.
>>
>>
>> Then when you click the HTML button
>> <https://imgur.com/4SD6abe>in the Blogger dashboard,
>> it shows you the same Lorem Ipsum content, but just
>> the raw HTML source.
>>
>>
>> My Django admin panel when creating a new blog post
>> just accepts plain text.  No HTML markup formatting.
>>  Here is a pic of my Django dashboard with a red
>> arrow pointing to where I am hoping to add an HTML
>> formatting bar <https://imgur.com/Pkcva1s>. Or does
>> Django not have an HTML formatting menu feature
>> helping blog contributors to format their content?  I
>> can't find it in the model field type / option doc
>> that I linked to in my original post.
>> Thank you.
>> On Wednesday, November 15, 2017 at 10:41:31 PM UTC-5,
>> Amitesh Sahay wrote:
>>
>> The HTML file in Django is parsed through
>> views.py if that is what you are looking for. For
>> Italics <em> tag should work. For strong text,
>> you may use <strong> tag. I hope that I have
>> understood your question correctly.
>>
>> Hello,
>>
>> Regards,
>> Amitesh Sahay
>>
>> primary :: *91-907 529 6235*
>>
>>
>>
>> On Thursday 16 November 2017, 6:51:03 AM IST,
>> drone4four <drone...@gmail.com> wrote:
>>
>>
>> The contents of my models.py looks like this:
>>
>> ||
>> fromdjango.db importmodels
>>
>>
>> # Create your models here.
>> classPost(models.Model):
>>         title =models.CharField(max_length=25 6)
>> pub_date =models.DateTimeField()
>>         image =models.ImageField(upload_to=' media/')
>>         body =models.TextField()
>>
>>
>> def__str__(self):
>> returnself.title
>>
>>
>> defpub_date_pretty(self):
>> returnself.pub_date.strftime('%A %d %B %Y @
>> %-I:%M:%S %p')
>>
>>
>> defsummary(self):
>> returnself.body[:350]
>>
>> Lines 5 through 8 initiate the model class
>> variables for my blog dashboard. My dashboard
>> looks like this <https://imgur.com/a/HTffL>.
>> There is a title, pub date, image and body. The
>> Udemy instructor suggests consulting the official
>> Django doc for field types/options
>> <https://docs.djangoproject.com/en/1.11/ref/models/fields/>.
>> I'm not sure I really understand most of it.
>> There is just so much information there. My
>> question for all of you: Which field option or
>> field type initiates an HTML parser for body
>> text? I mean, when I go to to create a new blog
>> post, how do I create rich text with HTML
>> formatting buttons like bold, underline and
>> italics? It's not really the buttons I care
>> about. I just want my HTML tags to parse. Take
>> note of the HTML tags I've circled in red here
>> <https://imgur.com/a/wV4qs>. How do I get the h5,
>> hr and em to parse? Is there a field option/type
>> for this? I don't see it in the models fields doc.
>>
>> Thanks for your attention.
>>
>> --
>> You received this message because you are
>> subscribed to the Google Groups "Django users" group.
>> To unsubscribe from this group and stop receiving
>> emails from it, send an email to
>> django-users...@googlegroups. com.
>> To post to this group, send email to
>> django...@googlegroups.com.
>> Visit this group at https://groups.google.com/
>> group/django-users
>> <https://groups.google.com/group/django-users>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/
>> msgid/django-users/d387add4-
>> 9859-4410-a882-cf98ee3a6278% 40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/d387add4-9859-4410-a882-cf98ee3a6278%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit
>> https://groups.google.com/d/ optout
>> <https://groups.google.com/d/optout>.
>>
>> --
>> You received this message because you are subscribed to the
>> Google Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from
>> it, send an email to django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at
>> https://groups.google.com/group/django-users
>> <https://groups.google.com/group/django-users>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/5f02db01-95ba-4a73-b903-7479646a4015%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/5f02db01-95ba-4a73-b903-7479646a4015%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>
>>
>> For more options, visit https://groups.google.com/d/optout
>> <https://groups.google.com/d/optout>.
>>
>> --
>> You received this message because you are subscribed to the
>> Google Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to django-users...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/django-users
>> <https://groups.google.com/group/django-users>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/58a7a563-48a2-415e-9cb2-d59330129745%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/58a7a563-48a2-415e-9cb2-d59330129745%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout
>> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e7adee57-6faf-41fe-af06-2929f442b2bc%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/e7adee57-6faf-41fe-af06-2929f442b2bc%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8ab73de0-e535-046f-2f2b-d77e22ef6875%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment