last day (16 days later) » 

5:30 PM
0
A: how do I make a model field's value be assigned to a variable and it should belong to the person logged in

VincentYou'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"> <i...

 
thanks for the help but one more thing you should know is that the estimated price quote is sent by admin...so is there a dynamic way to do this?
 
@violette what do you mean with 'send by admin'? please add the code how the price is rendered in the html
 
once a user submits a quote the admin then adds a price from the django admin and then that shows up in the user side...like shown in picture
the price is predefined but now I want so that the user when he clicks on the price (which is the link) that very price for that logged in user can be assigned to stripe amount argument
 
@violette it's hard to come up with a solution if the code is not provided but as the price_quote is shown there, my example should still work.
 
could you just tell me how can access that piece of data for the logged in user? piece of data which is the estimated_cost model field which has a foreign key field which links to django User model
 
5:30 PM
@violette add the code that handles the stripe request
 
hi
 
Can you share those models?
hi!
 
sure
thanks for taking the time to help
 
no problem! happy to help
 
user = models.ForeignKey(User, on_delete=models.CASCADE)
project_type = models.CharField(max_length=50, choices=PROJECT_TYPE)

back_end_framework = models.CharField(max_length=50, choices=BACK_END_FRAMEWORKS)
front_end_framework = models.CharField(max_length=50, choices=FRONT_END_FRAMEWORKS)

tell_us_more = models.TextField(max_length=1000)
estimated_cost = models.IntegerField(null=True)
def charge(request):
if request.method == "POST":
print('data', request.POST)
customer = stripe.Customer.create(
email = request.user.email,
name = request.user.first_name,
source = request.POST["stripeToken"]
)

charge = stripe.Charge.create(
customer=customer,
amount=13440,
currency='USD',
description="charge for project"
)

order = models.ProjectTracking.objects.create(
client=request.user,
project_type="Web Application",
project_name=None,
order_value=13440,
order_id=None,
status="IN-PLANNING",
this function is where the tricky stuff is happening
 
5:32 PM
yeah
 
i want the amount=(the_quote_price_admin_sent)
in stripe charge
 
ah yeah
and do you save the price quote that the admin sends?
 
yes
 
to the estimated_cost field?
 
yes
amount should be the estimated_cost which was clicked
in the template i made the estimated_cost a link but i want so that when an estimated cost is clicked the stripe charge amount argument is set to that quote price the user clicked
 
5:35 PM
you can access that field in your charge method like
@violette what's the name of this model
 
QuoteTool
 
okay
can you show how you render the html where you show the price and back_end and frontend
 
back end is basically rendering out the QuoteTool.objects.all().filter(user=request.user)
and front end is just showing that in a table
using jinja {{ }}
 
yeah, so to access the estimated_cost field, you'll need to know which instance of QuoteTool you need
 
how do I access that model field
for that user
estimated_cost
 
5:40 PM
you can add a hidden input with the id of the model field
and submit that with the request
and get in the charge method like you do with source = request.POST["stripeToken"]
 
any way to access that model field's data via views?
using objects.something.
 
you could yeah using the user
but that is less accurate
for example you could do
users_quotes = QuoteTool.objects.filter(user=request.user)
in the charge method
 
how do i just get the estimated cost?
 
and then you could pick last or first like QuoteTool.objects.filter(user=request.user).first()
 
ohh really?
 
5:44 PM
like, users_quote = QuoteTool.objects.filter(user=request.user).last()
estimated_cost = users_quote.estimated_cost
but the problem with this solution is that this can be wrong as the quote the user is seeing
might not be the first or last
 
i wish there was a way where i could just get what the user clicked and that estimated cost would be the charge amount
 
you could do that like I showed in my answer
 
explain how your way would fit in this code i showed you
 
you are showing the estimated_cost in your html right?
 
yes
 
5:48 PM
you could also send that value with the post request
 
how do i bring that very cost the user clicks and send it to the charge view inside of the amount=
 
like:
you did with request.POST["stripeToken"]
could be an hidden input
<input type="hidden" value="{{estimated_cost}}" name="price_quote"/>
in your charge method access it like: request.POST.get('price_quote')
 
so when the user clicks that estimated cost green button like you saw in the picture i attached in my question...how will it take that very price quote as there can be many
like 2 or 3 more
how does it get that very price and assign it to amount
 
like I just said
<input type="hidden" value="{{estimated_cost}}" name="price_quote"/>
add this to the html
in your charge method access it like: amount=request.POST.get('price_quote')
 
estimated_cost is stored in db
 
5:53 PM
estimated_cost is the value that the users clicks
 
ye
yes
 
could be any variable to you use
yeah
so send that value with the post request
like I just said
 
yes but value needs to be the one that user clicks
 
yeah, but how do you get that value there
 
if i click on a price quote which says $1000 then the charge views should be set as 1000 in the amount argument in stripe charge
 
5:55 PM
show me the html please
I need the html
 
{% if quote_requests %}
{% for quote in quote_requests %}
<tr>
<td>{{ quote.project_type }}</td>
<td>{{ quote.back_end_framework }}</td>
<td>{{ quote.front_end_framework }}</td>
<td>{{ quote.tell_us_more|truncatechars:50 }}</td>

{% if quote.estimated_cost %}
<td><span class="badge badge-success"><a href="{% url 'dashboard:buy' %}">${{ quote.estimated_cost }}<a/></span></td>

{% else %}
{% endif %}
{% endfor %}
{% endif %}
admin from django admin adds a cost and saves that and then only will the estimated cost show to user for his or her quote
 
yeah
so the value the user clicks is quote.estimated_cost
 
yes
exactly
 
this url: dashboard:buy goes to the charge method?
in the charge method you do if request.method == 'POST'
but you're not doing a post request
 
that goes to the page where there is a stripe form
to enter card info and then submit payment
that is then sent to the charge view
 
5:59 PM
okay
 
which has the stripe api for python running which takes amount, name etc..create charge
 
yeah
 
so how do i get that quote price and set it as the amount in charge view
 
you can add the quote id to the url
for example
{% url 'dashboard:buy' pk=quote.id %}
change your urls.py to something like
path('**url**/<int:pk>/', **view*, name='**name**'),
 
how do i get the estimated_cost field back to the stripe charge amount kwarg
ohhh can i do something like get_object_or_404?
 
6:04 PM
then charger view is: def charge(request, quote_id):
 
but how would that work then?
 
ill make a file with all the code and share it with you
wait a sec.
 
oh wait
if this is too lame and confusing i have another idea
user can just select from a form which has a drop down of all the estimated_costs, and he or she can select one and then proceed?
how would that work?
 
what does then proceed do?
shouldnt I make an example for your current problem?
I can do that now
 
select a estimated_cost from drop down form and then press buy and then add that value / cost selected to the amount stripe must charge
that sounds sane but how would i just get 1 field which is the estimated cost field come only in the model form?
 
6:07 PM
yeah you need the ID
ill make something
wait a sec
 
ok thanks :)
i appreciate it
 
6:20 PM
@violette here you say that from the quote the user clicks, it first goes to another page before going to the charge method where you need the estimated_cost
show me how you render that page and what the html looks like
i need to see all code that is involved here
 
i need estimated cost in charge method
 
i know
but I need to how you get to that method
can you share your project maybe?
 
i think i might have an idea
from what you told me
 
okay
 
let me try it then i will mark your answer as correct
 
6:29 PM
its hard to explain here
okay
let me know if it works
otherwise maybe you can zip your project to me and I can work on a solution for you
 

last day (16 days later) »