【问题标题】:django + session var not transferring between viewsdjango + session var不在视图之间传输
【发布时间】:2015-12-18 13:09:39
【问题描述】:

一旦我在我的主页视图中提交了我的表单,我们就会被重定向到我的下载视图,它对我的​​会话变量有一个错误的响应......为什么???我不确定如何解决这个问题,感觉我已经尝试了所有方法。

@login_required
def Home(request):
    form = TimeForm()
    search_times = []
    if request.method == "POST":

        # clear session data
        # for key in request.session.keys():
        #     test.append(request.session[key])

        start = request.POST['start_time']
        end = request.POST['end_time']
        start = time.mktime(datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S").timetuple())
        end = time.mktime(datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S").timetuple())


        start = re.split('.[0-9]+$', str(start))[0]
        end = re.split('.[0-9]+$', str(end))[0]
        search_times.append(int(start))
        search_times.append(int(end))

        request.session['search_times'] = search_times
        request.session.save()

    context = {
        'form': form,
        'search_times': search_times,
    }
    return render(request, "pcap_app/home.html", context)

def Download(request, path):
    test = []
    access_time = []
    search_times = []

    search_times = request.session.get('search_times', False)

    # testing timestamp directory listing
    files = sorted_ls(os.path.join(settings.MEDIA_ROOT, path))
    for f in files:
        time = os.stat(os.path.join(settings.MEDIA_ROOT, f)).st_mtime
        access_time.append(int(re.split('.[0-9]+$', str(time))[0]))

    context = {
        'sort': files,
        'times': access_time,
        'search': search_times,
        'test': test,
    }
    return render(request, "pcap_app/downloads.html", context)

home.html

<head>
    <script                    src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <script src="{{ STATIC_URL }}js/bootstrap.js"></script>
    {{ form.media }}
</head>
<body>
    <h1>pcap search</h1>

    {% if user.is_authenticated %}

    <form  action="/download/" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input id="submit" type="submit" value="Submit">
    </form>
    {{ search_times }}
    {{ test }}
        <a href="/logout/">Logout</a>

    {% else %}
        <a href="/login/?next={{ request.path }}">Login</a>

    {% endif %}
</body>

【问题讨论】:

  • 你在上面的代码中重定向到哪里?另外,请修正你的缩进。
  • 我的意思是在上面的视图中,重定向代码在哪里。此外,在POST 请求的情况下,将执行什么代码 sn-p。
  • 重定向代码在我的 home.html 中,表单的操作
  • 请把home.html的代码贴在表格所在的地方。
  • 完成。添加了 .html 文件

标签: django session views


【解决方案1】:

尝试保存会话

request.session.save()

【讨论】:

    【解决方案2】:

    错误地将home.html 模板中的action 属性设置为/download/

    当您提交表单时,POST 请求将提交到 /download/ 视图而不是 home 视图。因此,设置会话的代码永远不会执行。

    您需要将action 属性更改为您的home 查看网址。当你这样做时,如果POST 请求,变量search_times 将在会话中设置。

     <form  action="/home/url/" method="POST">
    

    home.html 中指定action 属性后,您需要在home 视图中重定向到/download/。然后在您的download 视图发生重定向后,您将在会话中访问search_times 变量。

    @login_required
    def Home(request):
        form = TimeForm()
        search_times = []
        if request.method == "POST":
            ...
            request.session['search_times'] = search_times # set the variable in the session
            return HttpResponseRedirect('/download/') # redirect to download page
    
        context = {
            'form': form,
            'search_times': search_times,
        }
        return render(request, "pcap_app/home.html", context)
    

    【讨论】:

    • TY :) 我删除了表单操作中的字符串并在我的视图中添加了一个 url 重定向
    猜你喜欢
    • 2019-04-05
    • 1970-01-01
    • 2013-03-09
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多