0

I am a newbie in django, and I need some examples on this code I am working on.

I have a textbox, and an output on the same template and this is the view for this:

def sessiontest(request):
    a=request.session.get('a',{})
    form=sform()
    return render_to_response('cart.html',{'form':form,'a':a},context_instance=RequestContext(request))

and the view for processing the data is this :

def sessiontest2(request):
 if request.method=="POST":
     form=sform(request.POST)
     if form.is_valid():
         request.session['a']=form.cleaned_data["name"]
         return HttpResponseRedirect(reverse(sessiontest))

It is working fine if i just need to store ONE value, but how do I allow my request.session['a'] to store multiple data everytime there is a new input such as {value1,value2,new_value,new_value} ? I need it to be in a format which I can display with a {% for i in a %} loop in my templates and be used to filter my models.

This is just a test code, later on I will need to store my product_id in it and use it to filter my items in this way:

items=models.products.objects.filter(pk=request.session["a"])

Please give me some hints on how this could be done.

ruddra
  • 50,746
  • 7
  • 78
  • 101
user2478240
  • 369
  • 3
  • 15

3 Answers3

2

You can use this {% for i in a %} IF you are passing a list in the template from the views. Like the query items=models.products.objects.filter(pk=request.session["a"]) will provide a list of the object over which you can iterate and display attributes. You can use a dictionary for this if you want to store multiple items. request.session['a']['value1'] = 'value1'

You can access the dictionary in templates by using

{% for key, val in dict.items %}
    <td>{{ key }}</td>
    <td>{{ val }}</td>
{%  endfor  %}

OR you can use for loop counter to differentiate between values while stroing like this

count=0 

for i in val: 
  count+=1   
  request.session[a+str(count)]=i

Then you can access it like request.session["a1"] and so on for other values Hope that helps

S.Ali
  • 664
  • 1
  • 5
  • 12
1

The best possible way is to create another model which will store the session id for that session and the model will have a manytomany relationships with your product object .

Models.py

class Storedsessionproducts(models.Model): products=models.ManytoManyField(Products,null=True,related-name="storedproducts")

Now in your view.py set your session id to the created Storedsessionproducts of that session # use session_id to retrieve the current Storedsessionproducts object and then pull out all products from it session_id=request.session.get('cart_id')

## assuming you have successfully created Storedsessionproducts
# add all created products in Storedsessionproducts
request.session['cart_id']=Storedsessionproducts.id
john
  • 61
  • 4
0

You'd want to store the data as a list of values. An easy way to do this is:

request.session.setdefault('a', []).append(form.cleaned_data['name'])

This sets an empty list as the default if the key 'a' does not exist in the session, and then appends the name to the new or existing list. You can query for these objects using the following query:

Products.objects.filter(id__in=request.session.get('a', []))
knbk
  • 52,111
  • 9
  • 124
  • 122