option to create new collection when add from detail page

This commit is contained in:
Your Name 2022-05-31 21:29:29 -04:00
parent 7988ec55e8
commit a7e369a4c1
2 changed files with 6 additions and 2 deletions

View file

@ -29,8 +29,9 @@
{% csrf_token %}
<select name="collection_id">
{% for collection in collections %}
<option value="{{ collection.id }}">{{ collection.title }}{% if collection.visibility > 0 %}🔒{% endif %}</option>
<option value="{{ collection.id }}">{{ collection.title }}{% if collection.visibility > 0 %}🔒{% endif %}</option>
{% endfor %}
<option value="0">新建收藏单</option>
</select>
<div>
<textarea type="text" name="comment" placeholder="条目备注"></textarea>

View file

@ -407,6 +407,9 @@ def add_to_list(request, type, id):
}
)
else:
collection = Collection.objects.filter(owner=request.user, id=request.POST.get('collection_id')).first()
cid = int(request.POST.get('collection_id', default=0))
if not cid:
cid = Collection.objects.create(owner=request.user, title=f'{request.user.username}的收藏单').id
collection = Collection.objects.filter(owner=request.user, id=cid).first()
collection.append_item(item, request.POST.get('comment'))
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))