【问题标题】:Django - After overriding the base_site.html, i was unable to publish my post and causing CSRF errorDjango - 覆盖 base_site.html 后,我无法发布我的帖子并导致 CSRF 错误
【发布时间】:2021-04-14 02:46:36
【问题描述】:

这是我的 base_site.html 代码

{% extends "admin/base.html" %}
{% load static %}
{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | 
{{ site_title|default:_('Admin') }}
{% endblock %}
{% block extrastyle %}
<style type="text/css">
    #branding h1{
        color: #fff !important;
    }
    #branding h1 a:link, #branding h1 a:visited {
        color: #fff !important;
    }
    .submit-row input{
        background-color: #00b388;
    }
    #header {
        background-color: #000000;
        color: #fff !important;
    }
    .module caption{
        background-color: #00b388;
    }
    div.breadcrumbs {
        background: #00b388;
    }
    .object-tools a.addlink{
        background-color: #00b388;
    }
    input[type=submit].default, .submit-row input.default{
        background-color: #00b388;
    }
</style>
{% endblock %}
{% block branding %}
<form  method="post">{% csrf_token %}
<h1 id="site-name"><a href="{% url 'admin:index' %}"><img src="{% static 'img/logo.PNG' %}" height="30px" /></a></h1>
{% endblock %}
{% block nav-global %}
{% endblock %}

    unfortunately i have added the <form> tag in the above code, just to over come with CSRF issue and it does.
    unfortunately, this approach gives me another error, 

也就是说,我的模型中有 3 个文件字段,尽管我在上传过程中提到了这些文件,当我点击提交时,它不起作用并一直显示此字段是必填错误。

    I am new to Django and your response helps me great.
    versions:-
    Django - 2.2
    python - 3.6.2

【问题讨论】:

  • 你能显示你的错误的完整回溯吗?
  • 您的表单标签未关闭。正常吗?
  • @EricMartin 如果我关闭表单标签,不幸的是我看到了 403 禁止错误..
  • 你有 403 的堆栈跟踪吗?
  • 嗨@EricMartin 关闭
    标签后,问题得到解决。非常感谢

标签: html css django overriding csrf


【解决方案1】:

你需要关闭表单标签:

<form  method="post">{% csrf_token %}
</form>
<h1 id="site-name"><a href="{% url 'admin:index' %}"><img src="{% static 'img/logo.PNG' %}" height="30px" /></a></h1>
{% endblock %}
{% block nav-global %}
{% endblock %}

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 2017-07-13
    • 2019-02-24
    • 2018-07-21
    • 2014-09-03
    • 2022-08-14
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多