Change a boolean value after a click on a button in Django
up vote
0
down vote
favorite
I need some help regarding button clicks and boolean value changes.
My model:
class Topic(models.Model):
subject = models.CharField(max_length=255)
category = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')
slug = models.SlugField(unique=True)
isReported = models.BooleanField(default=False)
startPrice = models.IntegerField(validators=[RegexValidator(r'^d{1,10}$')])
I want to change the state of the field "isReported" from the default value "False" to "True" after a click on a button:
<button type="submit" class="btn btn-primary btn-block">Report</button>
I already googled and found something like this on SO (How do I change a value in django on button click in HTML?) but I am lacking knowledge of the concept presented in this thread. Unfortunately this was the only helpful information I could find.
How can I change the boolean value in my database after a single button click? It is not neccessary to change the value back on this button.
I somehome tried to adapt the code of the link:
views.py:
def ajax_change_status(request):
isReported = request.GET.get('isReported', False)
pk = request.GET.get('pk', False)
# first you get your Job model
job = Topic.objects.get(pk=pk)
try:
job.isReported = isReported
job.save()
return JsonResponse({"success": True})
except Exception as e:
return JsonResponse({"success": False})
return JsonResponse(data)
xxx.html:
<form method="post" novalidate>
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm" id="change" role="button">Report</button>
</form>
<script>
$("#change").on('click', function () {
var pk = '{{ topic.pk }}'
var isReported = 'True'
$.ajax({
url: '/ajax/change_status/',
data: {
'isReported': isReported
'pk': pk
},
dataType: 'json',
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});
});
I am trying to understand the concept, but (obv.) it is not working.
POST /request/xxx-xxx-xxx-5622149-3/ HTTP/1.0" 200 5472
ajax django button boolean
add a comment |
up vote
0
down vote
favorite
I need some help regarding button clicks and boolean value changes.
My model:
class Topic(models.Model):
subject = models.CharField(max_length=255)
category = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')
slug = models.SlugField(unique=True)
isReported = models.BooleanField(default=False)
startPrice = models.IntegerField(validators=[RegexValidator(r'^d{1,10}$')])
I want to change the state of the field "isReported" from the default value "False" to "True" after a click on a button:
<button type="submit" class="btn btn-primary btn-block">Report</button>
I already googled and found something like this on SO (How do I change a value in django on button click in HTML?) but I am lacking knowledge of the concept presented in this thread. Unfortunately this was the only helpful information I could find.
How can I change the boolean value in my database after a single button click? It is not neccessary to change the value back on this button.
I somehome tried to adapt the code of the link:
views.py:
def ajax_change_status(request):
isReported = request.GET.get('isReported', False)
pk = request.GET.get('pk', False)
# first you get your Job model
job = Topic.objects.get(pk=pk)
try:
job.isReported = isReported
job.save()
return JsonResponse({"success": True})
except Exception as e:
return JsonResponse({"success": False})
return JsonResponse(data)
xxx.html:
<form method="post" novalidate>
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm" id="change" role="button">Report</button>
</form>
<script>
$("#change").on('click', function () {
var pk = '{{ topic.pk }}'
var isReported = 'True'
$.ajax({
url: '/ajax/change_status/',
data: {
'isReported': isReported
'pk': pk
},
dataType: 'json',
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});
});
I am trying to understand the concept, but (obv.) it is not working.
POST /request/xxx-xxx-xxx-5622149-3/ HTTP/1.0" 200 5472
ajax django button boolean
The answer on the thread you linked solves your problem perfectly. Can you expand on the concept(s) you don't understand?
– v25
Nov 7 at 20:25
hi v25, I edited the code with my adapted version. Any input appreciated!
– JohnDole
Nov 7 at 21:14
Firstly, in the javascrpit snippet, keep the line'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
as the AJAX request needs to pass this back for security reasons. However it's not obvious what's not working, as you haven't really posted any error traceback. The last line you added looks like a sucessful HTTP response (200). Are you seeing an error elsewhere?
– v25
Nov 7 at 21:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need some help regarding button clicks and boolean value changes.
My model:
class Topic(models.Model):
subject = models.CharField(max_length=255)
category = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')
slug = models.SlugField(unique=True)
isReported = models.BooleanField(default=False)
startPrice = models.IntegerField(validators=[RegexValidator(r'^d{1,10}$')])
I want to change the state of the field "isReported" from the default value "False" to "True" after a click on a button:
<button type="submit" class="btn btn-primary btn-block">Report</button>
I already googled and found something like this on SO (How do I change a value in django on button click in HTML?) but I am lacking knowledge of the concept presented in this thread. Unfortunately this was the only helpful information I could find.
How can I change the boolean value in my database after a single button click? It is not neccessary to change the value back on this button.
I somehome tried to adapt the code of the link:
views.py:
def ajax_change_status(request):
isReported = request.GET.get('isReported', False)
pk = request.GET.get('pk', False)
# first you get your Job model
job = Topic.objects.get(pk=pk)
try:
job.isReported = isReported
job.save()
return JsonResponse({"success": True})
except Exception as e:
return JsonResponse({"success": False})
return JsonResponse(data)
xxx.html:
<form method="post" novalidate>
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm" id="change" role="button">Report</button>
</form>
<script>
$("#change").on('click', function () {
var pk = '{{ topic.pk }}'
var isReported = 'True'
$.ajax({
url: '/ajax/change_status/',
data: {
'isReported': isReported
'pk': pk
},
dataType: 'json',
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});
});
I am trying to understand the concept, but (obv.) it is not working.
POST /request/xxx-xxx-xxx-5622149-3/ HTTP/1.0" 200 5472
ajax django button boolean
I need some help regarding button clicks and boolean value changes.
My model:
class Topic(models.Model):
subject = models.CharField(max_length=255)
category = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')
slug = models.SlugField(unique=True)
isReported = models.BooleanField(default=False)
startPrice = models.IntegerField(validators=[RegexValidator(r'^d{1,10}$')])
I want to change the state of the field "isReported" from the default value "False" to "True" after a click on a button:
<button type="submit" class="btn btn-primary btn-block">Report</button>
I already googled and found something like this on SO (How do I change a value in django on button click in HTML?) but I am lacking knowledge of the concept presented in this thread. Unfortunately this was the only helpful information I could find.
How can I change the boolean value in my database after a single button click? It is not neccessary to change the value back on this button.
I somehome tried to adapt the code of the link:
views.py:
def ajax_change_status(request):
isReported = request.GET.get('isReported', False)
pk = request.GET.get('pk', False)
# first you get your Job model
job = Topic.objects.get(pk=pk)
try:
job.isReported = isReported
job.save()
return JsonResponse({"success": True})
except Exception as e:
return JsonResponse({"success": False})
return JsonResponse(data)
xxx.html:
<form method="post" novalidate>
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-sm" id="change" role="button">Report</button>
</form>
<script>
$("#change").on('click', function () {
var pk = '{{ topic.pk }}'
var isReported = 'True'
$.ajax({
url: '/ajax/change_status/',
data: {
'isReported': isReported
'pk': pk
},
dataType: 'json',
success: function (data) {
if (data.success) {
alert("ajax call success.");
// here you update the HTML to change the active to innactive
}else{
alert("ajax call not success.");
}
}
});
});
I am trying to understand the concept, but (obv.) it is not working.
POST /request/xxx-xxx-xxx-5622149-3/ HTTP/1.0" 200 5472
ajax django button boolean
ajax django button boolean
edited Nov 7 at 21:13
asked Nov 7 at 19:07
JohnDole
356
356
The answer on the thread you linked solves your problem perfectly. Can you expand on the concept(s) you don't understand?
– v25
Nov 7 at 20:25
hi v25, I edited the code with my adapted version. Any input appreciated!
– JohnDole
Nov 7 at 21:14
Firstly, in the javascrpit snippet, keep the line'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
as the AJAX request needs to pass this back for security reasons. However it's not obvious what's not working, as you haven't really posted any error traceback. The last line you added looks like a sucessful HTTP response (200). Are you seeing an error elsewhere?
– v25
Nov 7 at 21:24
add a comment |
The answer on the thread you linked solves your problem perfectly. Can you expand on the concept(s) you don't understand?
– v25
Nov 7 at 20:25
hi v25, I edited the code with my adapted version. Any input appreciated!
– JohnDole
Nov 7 at 21:14
Firstly, in the javascrpit snippet, keep the line'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
as the AJAX request needs to pass this back for security reasons. However it's not obvious what's not working, as you haven't really posted any error traceback. The last line you added looks like a sucessful HTTP response (200). Are you seeing an error elsewhere?
– v25
Nov 7 at 21:24
The answer on the thread you linked solves your problem perfectly. Can you expand on the concept(s) you don't understand?
– v25
Nov 7 at 20:25
The answer on the thread you linked solves your problem perfectly. Can you expand on the concept(s) you don't understand?
– v25
Nov 7 at 20:25
hi v25, I edited the code with my adapted version. Any input appreciated!
– JohnDole
Nov 7 at 21:14
hi v25, I edited the code with my adapted version. Any input appreciated!
– JohnDole
Nov 7 at 21:14
Firstly, in the javascrpit snippet, keep the line
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
as the AJAX request needs to pass this back for security reasons. However it's not obvious what's not working, as you haven't really posted any error traceback. The last line you added looks like a sucessful HTTP response (200). Are you seeing an error elsewhere?– v25
Nov 7 at 21:24
Firstly, in the javascrpit snippet, keep the line
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
as the AJAX request needs to pass this back for security reasons. However it's not obvious what's not working, as you haven't really posted any error traceback. The last line you added looks like a sucessful HTTP response (200). Are you seeing an error elsewhere?– v25
Nov 7 at 21:24
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196177%2fchange-a-boolean-value-after-a-click-on-a-button-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The answer on the thread you linked solves your problem perfectly. Can you expand on the concept(s) you don't understand?
– v25
Nov 7 at 20:25
hi v25, I edited the code with my adapted version. Any input appreciated!
– JohnDole
Nov 7 at 21:14
Firstly, in the javascrpit snippet, keep the line
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
as the AJAX request needs to pass this back for security reasons. However it's not obvious what's not working, as you haven't really posted any error traceback. The last line you added looks like a sucessful HTTP response (200). Are you seeing an error elsewhere?– v25
Nov 7 at 21:24