picomon
picomon

Reputation: 1519

Django Session Not Matching Query

I want to make user order for services without logging in to do that. I'm using session to handle that. whereby user will have to fill a form before being redirected to third party payment website.

I wrote the below codes, it's working fine on development server. Both on Chrome and mozilla. After deploying to production server, I tested it on Chrome, it's working fine, but when I tested it on Mozilla, it will bring out this error:

DoesNotExist: Session matching query does not exist.

Models:

class Pay(models.Model):
    user=models.ForeignKey(User,blank=True, null=True)
    session=models.ForeignKey(Session)
    pub_date=models.DateTimeField()
    esell=models.ForeignKey(Esell)
    full_name=models.CharField(max_length=100)
    email_ad=models.EmailField(verbose_name="Email")
    phone_num=models.CharField(max_length=20)

    def __unicode__(self):
        return '%s %s' % (self.full_name, self.esell)

Views for accepting user data

@csrf_exempt
def pay_dig(request,esellid):
    if request.method=="POST":
        form=PayForm(request.POST)
        if form.is_valid():
            data=form.cleaned_data
            newpayy=Pay(
                session=Session.objects.get(session_key=request.session._session_key),
                pub_date=datetime.datetime.now(),
                esell=Esell.objects.get(pk=esellid),
                full_name=data['full_name'],
                email_ad=data['email_ad'],
                phone_num=data['phone_num'])
            newpayy.save()
            return HttpResponseRedirect('/confirm_payment/')
        else:
            return HttpResponse('Go back and fill the form correctly')
    else:
        return render_to_response('pay.html',{'PayForm':PayForm},context_instance=RequestContext(request))

Views for redirecting after filling the above form

def confam_pay(request):
    return render_to_response('after_pay_form.html',{'newpayy':Pay.objects.filter(session=request.session._session_key).latest('id')},context_instance=RequestContext(request))

Template

<p><strong> Details</strong> </p>
  <p> Your Name is {{newpayy.full_name }} </p>
<p> Price: N{{ newpayy.esell.up_price }} </p>
<p>  Name Of Product: {{ newpayy.esell.up_name }} </p>
<input type='image'  src='image/file' alt='Make Payment' />

What I'm I missing? or is there a better way to go about this?

Upvotes: 0

Views: 1954

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599530

I don't understand what you are doing with the session. Sessions aren't something you create foreign keys to - they might be stored in a db table, but they're ephemeral data associated with a user's current session.

Rather than storing the session in the Pay table, you should store the ID of the Pay model in the session.

Edit

Rather than trying to filter based on the session, I would store a list of Pay IDs in the session itself. Then to display purchases you can simply get all the instances by ID.

newpayy = Pay(...)
newpayy.save()
pay_list = request.session.setdefault('pay_list', [])
pay_list.append(newpayy.id)
request.session.modified = True

and to get the values back:

pay_ids = request.session.get('pay_list')
if pay_ids:
    pay_objects = Pay.objects.filter(pk__in=pay_ids)

Don't forget, whether you do it my way or your way, sessions are ephemeral and not reliable for long-term storage. If a user logs in or out, clears their cookies, or uses a different machine, the items in the session will be lost.

Upvotes: 1

Related Questions