首页 > 解决方案 > 当文件准备好时,在 Django 中的 UI 上显示字符串

问题描述

接下来我想做

当按下提交按钮时,Django会生成一些文件(这需要一些时间,大约一分钟)我希望提交按钮旁边会出现另一个按钮,以便在生成完成后(本地)下载这个文件。

屏幕截图: 图像

HTML:

{% load bootstrap %}

<!DOCTYPE html>
<html> 
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
      <title>Create SWM Client Environment</title>  
  </head>

  <body>
    <nav class="navbar navbar-dark bg-dark">
        <h4 style="color: aliceblue;">Create SWM client environment</h4>
    </nav>
    <div style="padding: 20px;">
        <form method="post">
            {% csrf_token %}
            <div class="form-group container w-25">
                {{ form|bootstrap }}
            </div>

            <div class="container w-25">
                <button type="reset" class="btn btn-primary">Reset</button>
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </form>
    </div>
  </body>
</html>

形式:

class ContactForm(forms.Form):
    swm_server = forms.CharField(
        label='server',
        widget=forms.TextInput(attrs={'placeholder': 'Server IP or Server DNS'}))

    def clean(self):
        cleaned_data = super(ContactForm, self).clean()
        swm_server = cleaned_data.get('swm_server')

意见:

def home(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation

    else:
        form = ContactForm()

    return render(request, 'home.html', {'form': form})

标签: pythonhtmldjango

解决方案


推荐阅读