python - django 在 render_to_response() 之后注销我

标签 python django render logout

这是我的views.py

def createpost(request, id=None):

    form = PostForm(request.POST or None, request.FILES or None)

    if form.is_valid() and request.method == 'POST':
        instance = form.save(commit=False)
        instance.user = request.user  

        instance.save()
        messages.success(request, "successfully create!")
        return HttpResponseRedirect('/post')
    else:
        context ={      
        'form': form
        }
    return render_to_response('createPost.html', context)

和我想要显示有错误的帖子页面的 createPost.html 代码

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <div class="alert alert-danger">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        <div class="alert alert-danger">
            <strong>{{ error|escape }}</strong>
        </div>
    {% endfor %}
{% endif %}

{% if request.user.is_authenticated %}\
    <form method="POST" name="PostForm" action="/post/createpost/" enctype="multipart/form-data"> {% csrf_token %}
        {{form|as_bootstrap_inline}}
        <input type="hidden" name="user_id" value="{{ user.id }}" />
        <button type="submit" class="btn btn-primary">Save AD</button>

{% else %}

    <div>Please Register First!</div>

{% endif %}

    </form>

{% endblock content %}

但是当错误发生时,它会重定向并注销我。如何将经过身份验证的用户发布到我的帖子页面? enter image description here

最佳答案

You should probably use render with request

rewrite your else part in views.py

例如 -

else:
        context ={      
        'form': form,

        }

        return render(request, 'createPost.htm', context)

关于python - django 在 render_to_response() 之后注销我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345277/

相关文章:

render - 为什么渲染字形这么慢?

python - 使用Python对图像使用最大似然算法进行分割

python - 基于共享 key 组合数据帧

python - Django 使用 Django 查询集获取所有后代子模型

php - 如何使用 PHP 下载_呈现_网页?

javascript - 等待在 AngularJS/通用 javascript 中应用更改?

python - Airflow ExternalTask​​Sensor 卡住了

python - 经过快速启动后,在访问 hadoop 时遇到一些问题

django - 移除 ForeignKey 后迁移到第三方模型

python - 如何在 Django View 中向另一台服务器发送请求?