python - 我无法显示文件上传按钮

标签 python html django

我无法显示文件上传按钮。 我在 profile.html 中这样写

{% extends "registration/accounts/base.html" %}
{% block content %}
user.username: {{ user.username }}<hr>
user.is_staff: {{ user.is_staff }}<hr>
user.is_active: {{ user.is_active }}<hr>
user.last_login: {{ user.last_login }}<hr>
user.date_joined: {{ user.date_joined }}
{% endblock %}

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>UPLOAD</title>


    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

{% block body %}
<div class="container">
  <form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple>
    <input type="hidden" value="{{ p_id }}" name="p_id">
    {% csrf_token %}
    <input type="submit">
  </form>
</div>
{% endblock %}

   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


  </body>
</html>

所以,我写了这样的 multipart/form-data 标签

 <form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">.

但是,profile.html 没有显示 ~ 的部分。

它只显示 {% extends "registration/accounts/base.html"%} ~ {% 端 block %}。

我该如何解决?

最佳答案

{% block content %} 的放置和位置在您的代码中是错误的。 您的代码有几个问题,我已将其删除。 你不需要额外的{% block body %}。 所以你应该改用这段代码:

  {% extends "registration/accounts/base.html" %}
  {% block content %}

  <!DOCTYPE html>

  <html lang="ja">
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>UPLOAD</title>
    </head>
    <body>
    user.username: {{ user.username }}<hr>
    user.is_staff: {{ user.is_staff }}<hr>
    user.is_active: {{ user.is_active }}<hr>
    user.last_login: {{ user.last_login }}<hr>
    user.date_joined: {{ user.date_joined }}

    <div class="container">
      <form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        <input type="file" name="files[]" multiple>
        <input type="hidden" value="{{ p_id }}" name="p_id">
        <input type="submit" value="Upload">
      </form>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </body>
  </html>

  {% endblock %}

另请注意,将 csrf_token 放在正确的位置并将 value="Upload" 添加到 input type="submit"

关于python - 我无法显示文件上传按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975223/

相关文章:

html - 我不能在表格中使用垂直滚动,而在这个表格所在的容器中使用水平滚动

python - 预期密集输入具有形状 (7,),但得到形状为 (1,) 的数组

python - 为特定的 python 版本安装 pip

javascript - 当用户突出显示文本时如何防止模态显示?

javascript - 检测函数内的垂直滚动和窗口宽度

python - base.html 中的 Django 变量

python - 使用 wsgi 和 virtualenv 时 django 错误 : could not import settings

Django写入数据库,IntegrityError

python - 让父方法返回子类实例

python - pandas 将 mysql utf-8 转换为 ascii