Python 通过选择或任何其他方法从用户端以html形式添加节/集群
假设我从用户提供的文本框中保存一个url,该url由用户提交并保存到我的模型中,现在我想做的是让用户添加不同的部分/集群,如技术,运动等假设他在那里输入URL,他可以定义该集群/部分,并将该特定URL保存到他在选项中创建或选择的特定部分/集群,该部分/集群已经存在!有没有人能帮我一下,或者给我举个小例子?使用django选项或其他方式做这件事的更好方法是什么 蒂亚Python 通过选择或任何其他方法从用户端以html形式添加节/集群,python,django,django-models,django-forms,django-3.0,Python,Django,Django Models,Django Forms,Django 3.0,假设我从用户提供的文本框中保存一个url,该url由用户提交并保存到我的模型中,现在我想做的是让用户添加不同的部分/集群,如技术,运动等假设他在那里输入URL,他可以定义该集群/部分,并将该特定URL保存到他在选项中创建或选择的特定部分/集群,该部分/集群已经存在!有没有人能帮我一下,或者给我举个小例子?使用django选项或其他方式做这件事的更好方法是什么 蒂亚 我将在下面粘贴当前代码: form.py 类UrlSaveForm(ModelForm): 类元: model=UrlSaveMod
我将在下面粘贴当前代码:
form.py
类UrlSaveForm(ModelForm):
类元:
model=UrlSaveModel
字段=['the_url']
models.py(如下)
类UrlSaveModel(models.Model):
_url=embeddevideofield()
desc=models.CharField(最大长度=200)
定义(自我):
返回self.desc
用于保存的HTML:save.HTML
{%extends'base.html%}{%block content%}
{%csrf_令牌%}
在此处输入URL以将视频保存到库:
保存URL
{%endblock%}
“”“和视图的小片段。py”“”
如果form.is_有效():
打印('表格有效')
posted\u url=request.POST['the\u url']
yt=YouTube(发布的url)
description=yt.title
打印('description-extracted:',description)
打印('Posted\u url',Posted\u url)
obj=UrlSaveModel(desc=description,the_url=posted_url)
obj.save()
返回HttpResponse('已成功保存!')
ill paste the current code below :
form.py
class UrlSaveForm(ModelForm):
class Meta:
model = UrlSaveModel
fields = ['the_url']
models.py (below)
class UrlSaveModel(models.Model):
the_url = EmbedVideoField()
desc = models.CharField(max_length=200)
def __str__(self):
return self.desc
HTML for saving: save.html
{% extends 'base.html' %} {% block content %}
<form method="post" action="{% url 'func' %}">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlTextarea1"
>Enter the URL here to save video to the library:</label
>
<textarea
class="form-control"
id="exampleFormControlTextarea1"
rows="2"
name="the_url"
placeholder="Enter the URL here"
></textarea>
<br />
<button type="submit" class="btn btn-primary mb-2">Save URL</button>
</div>
</form>
{% endblock %}
"""and little snippet of views.py"""
if form.is_valid():
print('form is valid')
posted_url = request.POST['the_url']
yt = YouTube(posted_url)
description = yt.title
print('description-extracted:', description)
print('Posted_url', posted_url)
obj = UrlSaveModel(desc=description, the_url=posted_url)
obj.save()
return HttpResponse('saved sucessfully!')