def new(request): initial_url = request.GET.get('url') initial_auto_close = 'auto_close' in request.GET if request.method == 'POST': form = BookmarkForm(request.POST) auto_close = form.data['auto_close'] if form.is_valid(): current_user = request.user create_bookmark(form.save(commit=False), form.data['tag_string'], current_user) if auto_close: return HttpResponseRedirect(reverse('bookmarks:close')) else: return HttpResponseRedirect(reverse('bookmarks:index')) else: form = BookmarkForm() if initial_url: form.initial['url'] = initial_url if initial_auto_close: form.initial['auto_close'] = 'true' all_tags = get_user_tags(request.user) context = { 'form': form, 'auto_close': initial_auto_close, 'all_tags': all_tags, 'return_url': reverse('bookmarks:index') } return render(request, 'bookmarks/new.html', context)
def edit(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) if request.method == 'POST': form = BookmarkForm(request.POST, instance=bookmark) return_url = form.data['return_url'] if form.is_valid(): update_bookmark(form.save(commit=False), form.data['tag_string'], request.user) return HttpResponseRedirect(return_url) else: return_url = request.GET.get('return_url') form = BookmarkForm(instance=bookmark) return_url = return_url if return_url else reverse('bookmarks:index') form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ') form.initial['return_url'] = return_url all_tags = get_user_tags(request.user) context = { 'form': form, 'bookmark_id': bookmark_id, 'all_tags': all_tags, 'return_url': return_url } return render(request, 'bookmarks/edit.html', context)
def edit(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) if request.method == 'POST': form = BookmarkForm(request.POST, instance=bookmark) if form.is_valid(): update_bookmark(form, request.user) return HttpResponseRedirect(reverse('bookmarks:index')) else: form = BookmarkForm(instance=bookmark) form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ') all_tags = get_user_tags(request.user) context = {'form': form, 'bookmark_id': bookmark_id, 'all_tags': all_tags} return render(request, 'bookmarks/edit.html', context)