简单的Django中的HTML表单

huangapple go评论71阅读模式
英文:

Simple html form in Django

问题

Sure, here's the translated content:

请你帮助我,我不明白为什么在Django中HTML+CSS不按预期工作。

我使用一个简单的表单来上传两个文件,但它看起来非常难看。

index.html 没有 id="upload_container"

<div class="page_thirdblock thirdblock">
 <div class="thirdblock__container _container">
  <form method="POST" enctype="multipart/form-data" class="upload1"> 
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_1">
    <button type="submit">上传文件 1</button>
  </form>
  <form method="POST" enctype="multipart/form-data" class="upload2">
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_2">
    <button type="submit">上传文件 2</button>
   </form>
 </div>
</div>

index.html 使用 id="upload_container"

<div class="page_thirdblock thirdblock">
 <div class="thirdblock__container _container">
  <form method="POST" enctype="multipart/form-data" class="upload1" id="upload_container"> 
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_1">
    <button type="submit">上传文件 1</button>
  </form>
  <form method="POST" enctype="multipart/form-data" class="upload2" id="upload_container">
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_2">
    <button type="submit">上传文件 2</button>
  </form>
 </div>
</div>

我该如何修复它?我的意思是如何使用CSS样式来获取“浏览...”按钮。

英文:

Could you please help me, I don't understand why html+css don't work as expected in Django.

I use a simple form to upload two files, but it looks really ugly.

简单的Django中的HTML表单

index.html without id="upload_container"

<div class="page_thirdblock thirdblock">
 <div class="thirdblock__container _container">
  <form method="POST" enctype="multipart/form-data" class="upload1"> 
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_1">
    <button type="submit">Upload File 1</button>
  </form>
  <form method="POST" enctype="multipart/form-data" class="upload2">
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_2">
    <button type="submit">Upload File 2</button>
   </form>
 </div>
</div>

简单的Django中的HTML表单

index.html with id="upload_container"

<div class="page_thirdblock thirdblock">
 <div class="thirdblock__container _container">
  <form method="POST" enctype="multipart/form-data" class="upload1" id="upload_container"> 
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_1">
    <button type="submit">Upload File 1</button>
  </form>
  <form method="POST" enctype="multipart/form-data" class="upload2" id="upload_container">
    {% csrf_token %}
    <input type="file" name="upload_file">
    <input type="hidden" name="form_type" value="form_2">
    <button type="submit">Upload File 2</button>
  </form>
 </div>
</div>

How I can fix it? I mean get Browse... button, but with css style.

答案1

得分: 1

尝试这个,看看会发生什么:

<div class="page_thirdblock thirdblock">
  <div class="thirdblock__container _container">
    <form method="POST" enctype="multipart/form-data" class="upload1" id="upload_container">
      {% csrf_token %}
      <label for="file1" class="file-upload-label">
        <input type="file" name="upload_file" id="file1" style="display:none;">
        上传文件1
      </label>
      <input type="hidden" name="form_type" value="form_1">
      <button type="submit">上传</button>
    </form>
    <form method="POST" enctype="multipart/form-data" class="upload2">
      {% csrf_token %}
      <label for="file2" class="file-upload-label">
        <input type="file" name="upload_file" id="file2" style="display:none;">
        上传文件2
      </label>
      <input type="hidden" name="form_type" value="form_2">
      <button type="submit">上传</button>
    </form>
  </div>
</div>
英文:

try this, see what happens

&lt;div class=&quot;page_thirdblock thirdblock&quot;&gt;
  &lt;div class=&quot;thirdblock__container _container&quot;&gt;
    &lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot; class=&quot;upload1&quot; id=&quot;upload_container&quot;&gt;
      {% csrf_token %}
      &lt;label for=&quot;file1&quot; class=&quot;file-upload-label&quot;&gt;
        &lt;input type=&quot;file&quot; name=&quot;upload_file&quot; id=&quot;file1&quot; style=&quot;display:none;&quot;&gt;
        Upload File 1
      &lt;/label&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;form_type&quot; value=&quot;form_1&quot;&gt;
      &lt;button type=&quot;submit&quot;&gt;Upload&lt;/button&gt;
    &lt;/form&gt;
    &lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot; class=&quot;upload2&quot;&gt;
      {% csrf_token %}
      &lt;label for=&quot;file2&quot; class=&quot;file-upload-label&quot;&gt;
        &lt;input type=&quot;file&quot; name=&quot;upload_file&quot; id=&quot;file2&quot; style=&quot;display:none;&quot;&gt;
        Upload File 2
      &lt;/label&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;form_type&quot; value=&quot;form_2&quot;&gt;
      &lt;button type=&quot;submit&quot;&gt;Upload&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月29日 23:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定