2009-11-24 57 views
2

我一直在使用django框架背面的Python构建的商店,一切正常,直到我注意到当用户进行结帐并被请求登录时在他们这样做和他们的篮子倒空...明显这对篮子来说并不是一件好事,我想知道是什么造成了这种情况,是否可以查看一下我的代码并给出一些建议?我不知道该怎么做。用户登录时丢失会话数据

=====编辑 - 下面是我的代码,我将不胜感激,如果有人可以给我的,我怎么能阻止篮下清算一击,当用户登录=====

def basket(request): 
    """ 
    Display the current state of the basket and allow the customer to modify 
    the discount and quantities of each row of the basket 
    """ 
    data = {} 

    basket = Basket(request) 

    discount_form = DiscountCodeForm(basket) 


    if request.method == "POST": 
     if 'update' in request.POST: 
      basket.post_update(request) 

      discount_form = DiscountCodeForm(basket, request.POST) 
      if discount_form.is_valid(): 
       cleaned_data = discount_form.cleaned_data 
       if cleaned_data['discount_code']: 
        basket.set_discount(Offer.objects.get(code=cleaned_data['discount_code'])) 

     if 'delete' in request.POST: 
      basket.post_delete(request) 

     if 'remove_discount' in request.POST: 
      basket.remove_discount() 

    data['discount_form'] = discount_form 
    data['logged_in'] = persistent_account(request) 
    data['pageclass'] = 'basket' 
    data['category'] = Category.objects.root_category() 
    data['products'] = Product.objects.all() 
    data['regions'] = Zone.objects.all() 
    data['currency'] = Currency.get_default_currency() 

    return render_to_response('basket.html', data, RequestContext(request)) 





def login(request): 
""" 
Log the user in. 
The form is where the actual login occurs. If already logged in, then 
forward to the last attempted page, or, if came directly to the login page, 
the account page. 
@todo: Incorrect guesses limit of 10 then deactive account 
""" 
data = {} 

redirect_to = request.GET.get('next', reverse('account')) 

account = persistent_account(request) 
if account: 
    return HttpResponseRedirect(reverse('account')) 

if request.method == "POST": 
    login_form = LoginForm(request, request.POST) 
    # This next line will also cause a login 
    if login_form.is_valid(): 
     login_form.user.message_set.create(message="You have successfully logged in. Welcome back.") 
     return HttpResponseRedirect(redirect_to) 
else: 
    login_form = LoginForm(request) 

data['shop_login_form'] = login_form 
data['pageclass'] = 'customer_login' 

return render_to_response('login.html', data, RequestContext(request)) 

我给你的是我的登录视图和篮子视图,如果不是随意喊我,那么这个视图就足够了。

+0

您没有发布任何代码... – 2009-11-24 19:20:08

+2

您是否正在使用自定义构建的购物车?的Satchmo?还有别的吗?你是否缩小了问题的根源?你在问什么? “来看看我的代码!”是咨询顾问的要求,咨询费用。 “为什么这段代码做错了什么?”是一个公平的问题,通常可以在这样的问题板上处理。 – jcdyer 2009-11-24 20:15:39

+1

与jcd一致......我们需要更多详细信息,例如它正在做什么,正在打印的任何错误以及您可以提供的任何调试信息。如果它自己的代码尝试打印正在传递给检出的输出,并查看是否有任何事情是痛苦的并导致数据消失。 – 2009-11-24 20:17:54

回答

1

你在同一台机器上运行两个django实例吗?如果是这样,检查SESSION_COOKIE_NAME设置为每个实例不同的东西。

我们遇到了使用会话使用相同的SESSION_COOKIE_NAME的实例具有非常零星(读奇异)行为的问题。

1

这可能与会话管理有关。

用户无需先登录即可到达您的网站,向购物篮中添加了几件东西,然后继续结帐。在第一次到达您的网站时,会为此用户建立一个会话。这可以通过cookie,URI中存在的会话ID或两者的组合来完成。会话将用户的购物篮与用户相关联,并在服务器上进行跟踪。

现在,为了签出您的系统,用户必须登录。这会为用户创建一个全新的会话,并且您的系统正在失去用户原有会话的跟踪。这样做的最终效果就是他们的篮子倒空 - 因为他们实际上有一个新篮子。

我完全不知道你的系统中的会话是如何管理的(因为除了django,你没有提供任何细节),但这是我开始寻找的地方。

我不认为你会发现任何人会为你做一个免费的代码审查,所以你需要弄清楚如何在你的系统中管理会话,然后发布一个更具体的问题。祝你好运。