首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django曲奇未被设定

django曲奇未被设定
EN

Stack Overflow用户
提问于 2016-03-18 07:48:09
回答 1查看 1.5K关注 0票数 0

我试图在主页上设置django测试cookie,但是它没有被设置,我已经尝试了从改变中间件类到会话引擎,我的索引函数

def索引(请求):

代码语言:javascript
复制
request.session.set_test_cookie()
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required
        # ...
        # redirect to a new URL:
        return HttpResponseRedirect('/thanks/')

# if a GET (or any other method) we'll create a blank form
else:
    form = NameForm()

return render(request, 'name.html', {'form': form})

下面是我用来测试cookie的另一个功能

def登记册(请求):

代码语言:javascript
复制
if request.session.test_cookie_worked():
    print ">>>> TEST COOKIE WORKED!"
    request.session.delete_test_cookie()
# Like before, get the request's context.
context = RequestContext(request)

# A boolean value for telling the template whether the registration was successful.
# Set to False initially. Code changes value to True when registration succeeds.
registered = False

# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
    # Attempt to grab information from the raw form information.
    # Note that we make use of both UserForm and UserProfileForm.
    user_form = UserForm(data=request.POST)
    profile_form = UserProfileForm(data=request.POST)

    # If the two forms are valid...
    if user_form.is_valid() and profile_form.is_valid():
        # Save the user's form data to the database.
        user = user_form.save()

        # Now we hash the password with the set_password method.
        # Once hashed, we can update the user object.
        user.set_password(user.password)
        user.save()

        # Now sort out the UserProfile instance.
        # Since we need to set the user attribute ourselves, we set commit=False.
        # This delays saving the model until we're ready to avoid integrity problems.
        profile = profile_form.save(commit=False)
        profile.user = user

        # Did the user provide a profile picture?
        # If so, we need to get it from the input form and put it in the UserProfile model.


        # Now we save the UserProfile model instance.
        profile.save()

        # Update our variable to tell the template registration was successful.
        registered = True

    # Invalid form or forms - mistakes or something else?
    # Print problems to the terminal.
    # They'll also be shown to the user.
    else:
        print user_form.errors, profile_form.errors

# Not a HTTP POST, so we render our form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
    user_form = UserForm()
    profile_form = UserProfileForm()

# Render the template depending on the context.
return render_to_response(
        'register.html',
        {'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
        context)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-18 09:38:41

您不需要对您的设置文件进行所有这些中间程序更改。我刚刚试着设置了一个简单的代码演示,它运行得非常完美。

首先,尝试将代码最小化到只设置cookie“试验”。你可以在这里试试这个演示。

一个视图,可能是一个主视图的索引,设置cookie。在另一个视图上,可能是另一个应用程序的索引视图测试cookie。

代码语言:javascript
复制
  From the docs you can only test using another page request I quote
  *test_cookie_worked()* "Returns either True or False, depending on whether the user’s    
   browser accepted the test cookie. Due to the way cookies work, 
   you’ll have to call set_test_cookie() on a previous, 
   separate page request. See Setting test cookies below for more information."

    project/appxx/views ... request.session.set_test_cookie() #inside a view
    project/appyy/views ...  if request.session.test_cookie_worked():
                               request.session.delete_test_cookie()
                               return HttpResponse(">>>> TEST COOKIE WORKED!")

然后,您可以在以后添加逻辑。我使用的是HttResponse方法,而不是您使用的打印状态。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36078844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档