0

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?

picomon
  • 1,479
  • 4
  • 21
  • 38
  • I found this http://stackoverflow.com/questions/5130639/django-setting-a-session-and-getting-session-key-in-same-view and followed the process. It worked, at least. :) – picomon Jan 02 '14 at 22:22

1 Answers1

1

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • How can I store Pay ID in session? :( – picomon Jan 02 '14 at 13:52
  • The reason why I used session is that after the user must have paid, upon redirection from the third party payment website, the user will be able to download what he paid for. – picomon Jan 02 '14 at 13:54
  • @limelights what did you want me to blog about? I did write something the other week about my fairly obscure AST-parsing vim extension, which I doubt is of interest to anyone but me. Haven't written about Django for a while since, despite my answers here, I'm not really using it on a day-to-day basis at the moment. That might change soon though. – Daniel Roseman Jan 02 '14 at 14:00
  • @picomon you can do `request.session['pay_id'] = newpayy.id` after saving it and then get the ID back from `request.session['pay_id']` when the user wants to download. – Daniel Roseman Jan 02 '14 at 14:01
  • So with your answer, it means I should remove session from models, session=models.ForeignKey(Session) and this from views. session=Session.objects.get(session_key=request.session._session_key). Will I be able to filter the data submitted by the user after payment? – picomon Jan 02 '14 at 14:05
  • Don't forget that it's not important for the user to login before making payemnt. Will it still work? :) – picomon Jan 02 '14 at 14:07
  • @DanielRoseman Nah, I don't know but I do enjoy reading your blog from time to time! Always love reading about people using Django. I would blog myself but I suck at writing and I suck even more at Django so a blog from me would be pointless :) – Henrik Andersson Jan 02 '14 at 14:11