I've been trying to make this portion of my website update whenever a button is pressed:
In my template, I access this information through {{ request.user.profile.coins }}
:
<span class="status">Balance: {{ request.user.profile.coins }}
<img class="coin-img" src="{% static 'assets/coin.png' %}" height="40px" width="auto">
</span>
I was looking into the process and I attempted to use an AJAX function to call this view:
@login_required(login_url='users/login')
def coin_increase(request):
"""
Function based view for increasing a user's coin balance
"""
if request.is_ajax():
try:
user = request.user
except User.DoesNotExist:
raise Http404("No user matches the given query.")
user.profile.coins += 5
user.save()
return render(request, 'home.html', {'home': home})
else:
raise Http404
The AJAX function is as follows:
function update_coins() {
$.ajax({
method: "POST",
url: "/coins",
data: {},
success: function(data) {
alert("test");
}
})
};
How can I get this to work?