[英]How to save tag in django to my model using django-taggit
I dont know how to save tag to my model ,can you plz guide me to do this我不知道如何将标签保存到我的模型中,你能指导我这样做吗
This is my model这是我的模型
class publish(models.Model):
Userid=models.ForeignKey(Userdata,on_delete=models.CASCADE)
tittle=models.CharField(max_length=20)
contend=models.TextField(max_length=20000)
select_cat=models.CharField(max_length=30,default='Horror')
tags=TaggableManager()
def __str__(self):
return self.tittle
And this is my form这是我的表格
class DisplayformNew(forms.ModelForm):
CHOICES=(('Horror','Horror'),('Education','Education'),('Sports','Sports'),('Story','Story'),('Comic','Comic'))
title=forms.CharField(label='Title',max_length=30)
text= forms.CharField(widget=forms.Textarea(attrs={"rows":15, "cols":50}))
sel_cat=forms.ChoiceField(choices=CHOICES,label='Select Catagoriy')
class Meta:
model=publish
fields=['title','text','sel_cat','tags']
And this is my views这是我的看法
def add_new(request):
form=DisplayformNew(request.POST)
if form.is_valid():
new_todo=publish(Userid=Userdata.objects.get(Email=request.user.email),tittle=request.POST['title'],contend=request.POST['text'],select_cat=request.POST['sel_cat'],tags=request.POST['tags'])
new_todo.save()
#form.save_m2m()
context={'form':form}
return render(request,'newpost.html',context)
I cant save Tag to my model ,can anyone help me to solve this ...... I can save it from admin view but how to save from user interface我无法将标签保存到我的模型中,谁能帮我解决这个问题......我可以从管理视图中保存它,但如何从用户界面保存
In your view.py在你看来.py
def add_new(request):
form=DisplayformNew(request.POST)
if form.is_valid():
new_todo=publish(Userid=Userdata.objects.get(Email=request.user.email),tittle=request.POST['title'],contend=request.POST['text'],select_cat=request.POST['sel_cat'],tags=request.POST['tags'])
new_todo.save(commit=false)
form.save_m2m()
context={'form':form}
return render(request,'newpost.html',context)
Change it accordingly to what I did here根据我在这里所做的相应更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.