4

I am trying to create an input form field which can take text, images or videos as we see in Twitter.

I am trying the following:

from django import forms
from .models import Tweet

class TweetModelForm(forms.ModelForm):
    content = forms.CharField(label='',
                widget=forms.Textarea(
                        attrs={'placeholder': "Your message",
                            "class": "form-control"}
                    ))

Which produces enter image description here

However, I am trying to use the same input box so that user can upload image, video, GIFs or just type text in the box. I am not sure how to modify the above form field.

Edit: If it is not possible using Django, can this be done using HTML5?

1
  • To clarify, do you mean you want to display the image when the user uploads it? Commented Apr 6, 2019 at 13:58

3 Answers 3

4
+25

enter image description here

You should use a WYSIWYG Editor for that like Froala Editor

Usage

from django import forms
from froala_editor.widgets import FroalaEditor

 class PageForm(forms.ModelForm):
     content = forms.CharField(widget=FroalaEditor)
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried using a WYSIWYG plugin? Django summernote might work https://github.com/summernote/django-summernote

You could then set the widget to SummernoteWidget in the form

Comments

0

For adding file upload in Django form you need to add FileField into your form class.

Update your forms.py as below.

from django import forms
from .models import Tweet

class TweetModelForm(forms.ModelForm):
    content = forms.CharField(label='',
                widget=forms.Textarea(
                        attrs={'placeholder': "Your message",
                            "class": "form-control"}
                    ))
    file = forms.FileField(label="Upload file", blank=True)

This would render separate file field that user could upload images and other files.

1 Comment

This method can upload the file but it doesn't create twitter kind of box where the image shows up in display immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.