擅长:python、mysql、java
<p>根据<a href="https://docs.djangoproject.com/en/2.1/topics/http/sessions/" rel="noreferrer">documentation</a>:</p>
<blockquote>
<p>SessionStore.create() is designed to create a new session (i.e. one
not loaded from the session store and with session_key=None). save()
is designed to save an existing session (i.e. one loaded from the
session store). Calling save() on a new session may also work but has
a small chance of generating a session_key that collides with an
existing one. create() calls save() and loops until an unused
session_key is generated.</p>
</blockquote>
<p>意味着使用<code>create()</code>而不是<code>save()</code>更安全。所以你可以这样尝试:</p>
<pre><code>if not request.session.session_key:
request.session.create()
session_id = request.session.session_key
</code></pre>