简体   繁体   中英

how do I make a model field's value be assigned to a variable and it should belong to the person logged in

I am building a services app using django and I am using stripe. The problem is that the logged in user is given a price quote and so he or she has the option to click on the price quote which will lead them to the test stripe payments page. everything works such as making the charge and etc but how do i get that specific number (price quote) which the user clicked on? that is an integer model field but I want it to be assigned to the stripe charge amount when a user clicks on it. 在此处输入图像描述

the green button is clicked but I want that integer (quote price) of the user making the request (user is a foreign key in the quotes model) to be assigned to the stripe charge amount

how would that happen?

You'll need a view that handles the stripe request. The quote price needs to be accessible within that view.

You can create a form for each price quote and submit it if the user clicks the price quote. For example:

price_quote.html

<form action="{% url 'pay_using_stripe' %}" method="post">
    <input type="hidden" name="price_quote" value="{{ price_quote }}"/>        
    <input type="submit" value="{{ price_quote }}"/>
</form>

urls.py

urlpatterns = [
   path('pay-using-stripe/', PayUsingStripe.as_view(), name='pay_using_stripe')
]

views.py

class PayUsingStripe(View):
    def post(self, request):
        price = self.request.POST.get('price_quote')
        """
          do the stripe request
        """

You could also use javascript and AJAX to submit a POST request to the url with the view that handles the stripe request with the price_quote as the body of the request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM