'how to create tag field in django form like youtube have

i want to create a tag field like youtube give tage field while uploading a vedio this is what i tried in in my blog form

my models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
# Create your models here.
class Blog(models.Model):
    author = models.OneToOneField(User, on_delete=models.CASCADE,)
    title = models.CharField(max_length=200,blank=False,)
    thumbnail = models.ImageField(upload_to='blogs_thumbnail',default='blogdefa.png')
    tags = models.CharField(max_length=500, blank=False, default='Blog')
    data = models.TextField(blank=False,)
    published_date = models.DateTimeField(default=timezone.now,editable=False)
    update_at = models.DateTimeField(auto_now=True,editable=False)

    def __str__(self):
        return self.title

and this what i want

any idea how to do it i don,t know how to do it my forms.py from django import forms from django.forms import ModelForm, Textarea from django.contrib.auth.models import User from .models import Blog, comment, report

forms here

class BlogForm(forms.ModelForm):
    class Meta:
        model = Blog
        fields = '__all__'
        widgets = {'data': Textarea(attrs={'cols': 80, 'rows': 20, 'placeholder':'Write Here'}),
                   'title':forms.TextInput(attrs={'placeholder':'Your Blog Title Here'}),
                   'tags': forms.TextInput(attrs={'placeholder':'Please enter you content related tags'}),
                    }
        exclude = ['author','published_date','update_at']

all i want is user can create his own tag for blogs like in youtube and not like stackoverflow where you have use to choose you tag please help currently it look like this which is not cool



Solution 1:[1]

First thing is that tags work. So to get them working you should relate it to your post. So you should create a Tag model and use a ManytoManyRelated field to relate tags because you need to get to the post/result at the end using tags.

from django.db import models
from django_extensions.db.fields import AutoSlugField
from django.db.models import CharField, TextField, DateField, EmailField, ManyToManyField


class Tag(models.Model):

    name = CharField(max_length=31, unique=True, default="tag-django")
    slug = AutoSlugField(max_length=31, unique=True, populate_from=["name"])

    def __str__(self):
        return self.name


class YourPost(models.Model):

    name = CharField(max_length=31, db_index=True)
    slug = AutoSlugField(max_length=31, unique=True, populate_from=["name"])
    description = TextField()
    date_founded = DateField(auto_now_add=True)
    contact = EmailField()
    tags = ManyToManyField(Tag, related_name="tags")

    class Meta:
        get_latest_by = ["date_founded"]

    def __str__(self):
        return self.name

Go on from here. Create serializers, Viewsets. Relate your tags to your post.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Innomight