django saving product id when clicking on a button - django

I have a ListView that displays a list of results (eg products) which are looped through in the template. Each product in the list has a button which the user can choose to select the product.
I would like to store the product id selected by the user in the session so I can use it later. (It will be used in the next view where the user will sign to pay for item and also to pass into external api request to check stock)
What's the best way to store the product id in the session for use later
Currently I tried this in the template:
<a class="btn btn-cta-primary" href="{% url 'users:signup' price_id=quote.priceId %}">Proceed</a>
Which I thought would pass it to the next view where I could write it to the session with
self.request.session['price_id']
(perhaps it's best to immediately save it to the session when the button is clicked?)

You can use javascript to store session. Below is an example to store session using jquery
$('#id_of_button').on('click', fucntion(e){
sessionStorage['product_id'] = 'PRODUCT_ID'
})

Related

How to pass context object from a template to a view in django?

In search results page,a list of experts with their name and some other details are displayed.The name and the details are accessed from a context object "expert" that has been passed to search results page.Beside each search result I also generated a "save" button .When the user clicks on the save button,I need to save the individual expert data to a database.How do I send this "expert" context object from the search results page to a view so that I can then save it to the database?
Here is the search result page:
Here is part of the code of the for the search results:
You can see I tried to create a hidden form with a button to send the context object to a view but it's not working.How do I create the button so that when the user clicks "save" button the url sends the context "expert" to a view?
I tried to create a hidden form with a button to send the context object to a view but it's not working

amp-form session based backend and 3rd party cookies

Trying to grok this e-commerce scenario...
I build an amp product page in amp that has the new amp-form
The add to cart button is an XHR to my backend (that is session based, using
cookies by default)
User searches for product and results take them
to my amp product page, but they've never been to my site
They submit the add to cart form
the CORS preflight makes it's way to my backend, and i set all the correct allows as per https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md
Now the actual request is made... backend initializes a session,
returns session identifier as a cookie, but since user never went to
my site...just the google amp cache it's treated as a 3rd party
cookie and browser discards it (cause user disables 3rd party cookies)
users session is lost, as is their add to cart action
So the question is, how do i keep the session around and the item in the cart?
Am i missing something? is there a trick i'm not seeing?
appreciate any insights.
Associating the shopping cart with the CLIENT_ID would be the best way to solve this problem. Unfortunately, transferring the CLIENT_ID via forms is not yet supported in AMP. It's currently being implemented, you can watch this issue for the current status.
Here is an approach that works right now: the idea is to encode the shopping cart content into a string that is returned in the form result. This way we can generate "View Cart" and "Checkout" links including the shopping cart content. Once the user clicks on one of those links, you can create the actual shopping cart in your backend and store the user id in a cookie.
For example:
<form action-xhr="/add-to-cart" method="POST">
<input type="hidden" name="itemId" value="headphones-123">
<!-- Hide after form submit success -->
<input type="submit" name="add" value="Add to Cart">
<div submit-success>
<template type="amp-mustache">
<!-- shopping cart contents, e.g headphones-123 -->
{#shoppingCartContent}
View In Cart
Checkout
{/shoppingCartContent}
</template>
</div>
<div submit-error>
<template type="amp-mustache">
{{message}} <!-- e.g. Only 2 Headphones are left. -->
</template>
</div>
</form>
The disadvantage of this approach is that the shopping cart will be lost when the user leaves the page without viewing the cart first. This will be solved once the CLIENT_ID can be passed via amp-form.
I also know very limited info about AMP pages but I suggest that you please read through the use of User identification and try using an AMP-generated client ID. As mentioned in the documentation:
By default, AMP will manage the provision of a client ID whether the page is accessed from the publisher's original website or through a cache.
Likewise, learn more about client ID substitution, including how to add an optional user notification ID, in Variables supported in AMP analytics.
Hope that helps!

Not able to make the navbar items dynamic in ember

I have navbar component which I have placed in an application.hbs so that it should be visible always.But I want to change the title of the navbar with each page I visit ( say I am on index page it should say "Home" , if I am on profile page it should say profile,etc).Right now what is happening is navbar title always remains "Home" for all the page.This is happening because the navbar gets rendered onlu during the time it loads the page in browser and after that it doesn't change according to page.
application.hbs
{{top-navbar dp_url=model.profile.dp_url first_name=model.profile.first_name title=title}}
{{outlet}}
Here the I am computing the value of title depending upon which page the user is.
application.js(controller)
if (currentPage === "" || currentPage === "#"){
currentState.set('title',"Home")
}
else if(currentPage === "Userprofile"){
console.log('myStudio');
currentState.set('title',"UserProfile");
}
In here the currentpage has the current url of app and I am comparing it and deciding the value of title for navbar.
But the top-navbar title value gets calculated only for the first time when user load the app in browser and not when I move ffrom one route to another.
I have also tried the Ember.Evented but not able to solve it.
I don't know what Ember.js and Ember-data version are you using, currentState is deprecated since 2.1, anyway looks like you're using a private method intended for Ember internals and not meant to be used in an application.
A possible (but maybe unnecessary complicated) way to accomplish what you want is:
Create a model with the information you want to mutate in the navbar (e.g. navbar-data).
In the route where the navbar is rendered, create and return a record for it using a fixed numeric ID (e.g. store.createRecord('navbar-data', { id: 1, title: "index" })).
Pass the created record to the component (instead of just a string).
Whenever you want to change, peek the record with store.peekRecord('navbar-data', 1) and change the value you want to change in the navbar.
Of course, the record you use for that must not be saved with record.save().

How to hide a View parameter from the URL in Django?

I have a view which displays the objects of a model. The objects are sorted using a key which I want to have as a view parameter so that I can create a system where the user gets to pick on how to sort the query sets.
I currently have it set up as follows:
def comics(request, sorting_key):
comics = Comic.objects.all().order_by(sorting_key)
...
However, the problem is that I need to include the sorting key somewhere in the url as well, and I don't want that. Is there any way to get around this problem? Or am I stuck with an URL that explicitly shows the sorting key?
You can make use of cookies, you can set cookie on client browser when user changes the parameter of sorting and reload the page to sort the data, which will call the same view again, read the cookie from request in your view and sort on that parameter.
Do take care of default parameter when cookie is not set or sorting on that key is not available, because user can change or delete the cookie.
You can replace the <a> tags in the menu links with <form> tags with the same URL, and send the sorting key using post, and read it from request.POST in the view.

Django: How do I position a page when using Django templates

I have a web page where the user enters some data and then clicks a submit button. I process the data and then use the same Django template to display the original data, the submit button, and the results. When I am using the Django template to display results, I would like the page to be automatically scrolled down to the part of the page where the results begin. This allows the user to scroll back up the page if she wants to change her original data and click submit again. Hopefully, there's some simple way of doing this that I can't see at the moment.
It should already work if you provide a fragment identifier in the action method of the form:
<form method="post" action="/your/url#results">
<!-- ... -->
</form>
and somewhere below the form, where you want to show the results:
<div id="results">
<!-- your results here -->
</div>
This should make the page jump to the <div> with ID results.
It is complete client site and does not involve Django, JavaScript or similar.
You need to wrap your data into something like this:
<div id="some-id">YOUR DATA TO BE DISPLAYED</div>
and if you make redirect in your view you need to redirect to url: /some-url/#some-id
if you don't make redirect you need to scroll to the bottom using javascript (but note that redirect is preffered way to use in view after saving data).