-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeAvailableorNot.html
68 lines (62 loc) · 2.5 KB
/
CodeAvailableorNot.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<div class="form-group">
<label for="id_code" class="control-label">Code</label><sup class="text-danger">*</sup>
<input type="text" name="code" class="form-control" placeholder="Enter Code" autocomplete="off" id="id_code"
maxlength="200" required="">
<span id="codeExistingMsg" style="background: #e7e7e7;"></span>
</div>
<!-- JS -->
<script>
$(document).on('keyup', '#id_code', function (e) {
e.preventDefault();
let targetElement = $('#codeExistingMsg');
{#let targetElement = document.getElementById('codeExistingMsg');#}
let keyword = $.trim($(this).val());
let url = '{% url "module:search_code" %}';
if (keyword !== '') {
let csrftoken = $("[name=csrfmiddlewaretoken]").val();
$.ajax({
url: url,
type: 'post',
data: {
'keyword': keyword,
},
dataType: 'html',
beforeSend: function (xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
},
success: function (data) {
{#targetElement.innerHTML = data;#}
targetElement.html(data);
{#targetEleent.html('<span class="text-info"><i class="fa fa-spinner"></i> This Code Is Available...</span>');#}
},
error: function (error) {
console.log(error);
}
});
} else {
targetElement.html(' Code Search must not empty');
}
});
</script>
<!-- URL -->
url_patterns = [
path("get_code/", GetCodeIDBySearch.as_view(), name="search_code")
]
<!-- Template(In Django) -->
class GetCodeIDBySearch(View):
def post(self, request):
if request.is_ajax():
keyword = request.POST['keyword']
model_code = Model.objects.only('code').filter(
Q(code__iexact=keyword.strip())
)
if not model_code:
return HttpResponse(
keyword + ' is available <span class="text-success"> <i class="fa fa-check"></i></span>')
else:
return HttpResponse(
keyword + ' is already used. try another one. <span class="text-danger"><i class="fa fa-times"></i></span>')
else:
return HttpResponse('You did something wrong...')