How to pass context object from a template to a view in django? - 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

Related

Can I generate a form depending on the results of a view?

I need to create a form which depends on the results of a view. I can generate it within the view and/or create a view template in order to do this....But how do I process it?
How do I connect my form/template generated form to a form_submission function?
When this custom/dynamic form is submitted I need to call some drupal functions to create some content in the site.
I did not find any way to do it in a view, but what I did was:
- Create a form programatically, making only a hidden field for NIDS
- Place my view next to that form using AJAX
- When the view is updated I copy the results of the view to the NIDS field and submit the form (This reloads the page)
- Form submission has 2 behaviours, 1 to re-generate the form when provided with NIDs, and other one for the actual form created by the NIDS result of my view.
Just in case this helps someone

django saving product id when clicking on a button

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'
})

Django redirect to form page and submit the form?

Right now. I have a search function in my page to search for item id. When I click search, I will render the same page with the result items and show item. And in other pages where I also display the item id, I want to add a link to the id to go to the same page where I search for that id.
Example: id: 123, I want the same page when:
1. search '123' in my search page(my search only accept exact match)
2. In other pages, click '123', go to the search page with results
How should I achieve this, I have tried many ways which don't wok.
You need to make use of the GET method that HTML forms provide. When you perform a search from the first page, you must make sure that you are doing so using the GET method in the form. This will append the form data into the URL.
E.g. If you have a 'name' field in your form which has 'John' inputted. The submission of this form will compose a URL like so:
http://someurl.com/?name=John
This can then be accessed using the Django request object:
name = request.GET['name']
You've probably done something similar already for displaying your search results. So, all you need to do is create a link in your second page that redirects to the search page with GET request variables appended.
E.g.
<a href="{% url 'search_page' %}?searchterm=232> Item 232 </a>

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 admin Add custom button + custom form

I want to add a custom button near 'Add model_name'. When i click on the newly created button i should like to show a custom form where I cans elect a model out of a select box. When i click on save I want to save this model and chance some parameters so it's a 'add' but without selecting all options again. I give a clear example:
I have a model names 'Book'. The first time i create a new book entry, i have a form 'add Book' and i have to fill out the form completely. So i have the book with primary key = Book_1_1 But now i want to add a second book, it's the same book as the first 1 BUT the version changed, so i want a new book but i don't want to select all items anymore in the standard 'add Book' form, i want something like i click on create new instance --> i can select 1 book out of a select box with all book objects in it, and when i 'save' this a new instance of the book gets generated. This instance has the following primary key: Book_1_2 for example. I know how to save this, but i don't know how to change the admin site to do this. I need 2 things:
1) add a button 'new instance' near 'Add_model_name'
2) Deliver a form with all model_name objects in a select box and when i click save i want to retrieve an object with which i can modify some things to save it as 'new book'.
Any ideas?
UPDATE I already added the 'new' button, but like i can see at this moment instead of the url = add , i have to create a new url inside the admin like add_instance etc. Does someone have any documentation on this?
Regards,
Hein
Making it way too hard on yourself. Just do this:
class MyModelAdmin(admin.ModelAdmin):
# Other stuff here
save_as = True
Now you can open up your book entry, change whatever is different and hit "Save as new" and it'll create a new book with that info instead of overwriting the other.