0

forms:

class TransactionForm(forms.ModelForm):     
    CHOICES = ((1, 'Buy'), (2, 'Sell'),)

    coin = forms.ModelChoiceField(queryset = Coin.objects.all()) 
    buysell = forms.ChoiceField(choices = CHOICES)

    field_order = ['buysell', 'coin', 'amount', 'trade_price']

    class Meta:
        model = Transaction
        fields = {'buysell', 'coin', 'amount', 'trade_price'}

    def __init__(self, coin_price = None, user = None, *args, **kwargs):
        super(TransactionForm, self).__init__(*args, **kwargs)
        print("Transaction form init: ", user, coin_price)

        if user:
            self.user = user
            qs_coin = Portfolio.objects.filter(user = self.user).values('coin').distinct()
            print("qs_coin test: {}".format(qs_coin))
            self.fields['coin'].queryset = qs_coin

        if coin_price:
            print("coin price test")
            self.coin_price = coin_price
            self.fields['price'] = self.coin_price

jquery snippet:

    $('#id_buysell').on('change', function(){

            console.log("buysell");
            console.log($('#id_buysell').val());

            var $formData = $(this).attr("id_buysell");
            console.log($formData.val());

            $.ajax({
                method: "GET",
                url: "/myportfolio/add_transaction",
                data: $formData,
            });

        });

    $('#id_coin').on('change', function(){

        console.log("coin change")

        var $formData = $(this).attr("id_coin");
        console.log($formData.val());

        $.ajax({
            method: "GET",
            url: "/myportfolio/add_transaction",
            data: $formData,
        });

    });

views snippet

def add_transaction(request):
    print(request.method)
    print("test1")

    print(request.GET)

    if request.method == "GET":
        if request.is_ajax():
            print("ajax test")

            data = {
                'test': "test1"
            }

            form = TransactionForm(user = request.user, coin_price = GetCoin("Bitcoin").price)

            return JsonResponse(data)

In my jquery functions I declare

var $formData = $(this).attr("id_coin");

and then pass it to the view with GET. However I'm not sure how to actually get this data in my view.

So how can I access this value? If I wanted to print it to see this value could I do something like this or similar?:

print(request.GET['coin'])

1 Answer 1

1

You can't send data that way via ajax, you need to send it like the following {key:'value',key2:'value2'}

var coin = $(this).attr("id_coin");

$.ajax({
    method: "GET",
    url: "/myportfolio/add_transaction",
    data: {
        id_coin:coin // retrrieve the value in python with the key
    },
});

In your views:

print(request.GET.get('coin'))
>>> # The value of coin from JS ajax
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.