【问题标题】:How to Receive Data From a Django Form Using a POST Request and render on Html template如何使用 POST 请求从 Django 表单接收数据并在 Html 模板上呈现
【发布时间】:2019-09-15 21:53:04
【问题描述】:

如何使用 POST 请求从 Django 表单接收数据并在 html 模板上呈现。我的查看代码是:

def my_drive_fun(request):

    if request.method =='POST':
        form=my_drive_module(request.POST)
        if form.is_valid():
            Production_Drive = form.cleaned_data['Production_Drive']
            work_drive = form.cleaned_data['work_drive']
            show_name = form.cleaned_data['show_name']
            show_sup = form.cleaned_data['show_sup']
            show_coor = form.cleaned_data['show_coor']
            glow_project="Projects"+show_name
            pro_drive_path=Production_Drive+":"+"\\"
            wor_drive_path = work_drive + ":" + "\\"
            #text=form.cleaned_data['post']
            print("Production_Drive:",Production_Drive,"Work Drive:",work_drive,show_name,show_sup,show_coor,pro_drive_path,wor_drive_path)
    form=my_drive_module()
    drive_data={'form':form}
    return render(request,'my_drive.html',drive_data)

【问题讨论】:

    标签: django django-rest-framework django-forms django-templates django-views


    【解决方案1】:

    你可以这样做:

    return render(request,'my_drive.html',locals())
    

    在模板中

    {{work_drive}} / {{show_name}}
    

    或者通过like传递

    context = {"data": form.cleaned_data}
    return render(request,'my_drive.html',context)
    

    在模板中

    {{data.work_drive}} / {{data.show_name}}
    

    所以你的视图代码会是这样的

    def my_drive_fun(request):
    
        if request.method =='POST':
            form=my_drive_module(request.POST)
            if form.is_valid():
                Production_Drive = form.cleaned_data['Production_Drive']
                work_drive = form.cleaned_data['work_drive']
                show_name = form.cleaned_data['show_name']
                show_sup = form.cleaned_data['show_sup']
                show_coor = form.cleaned_data['show_coor']
                glow_project="Projects"+show_name
                pro_drive_path=Production_Drive+":"+"\\"
                wor_drive_path = work_drive + ":" + "\\"
                #text=form.cleaned_data['post']
    
                context = {"data": form.cleaned_data}
                return render(request,'my_drive.html',drive_data)
        form=my_drive_module()
        drive_data={'form':form}
        return render(request,'my_drive.html',drive_data)
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      def my_drive_fun(request):
          form=my_drive_module(request.POST or None)
      
          if form.is_valid():
              Production_Drive = form.cleaned_data['Production_Drive']
              work_drive = form.cleaned_data['work_drive']
              show_name = form.cleaned_data['show_name']
              show_sup = form.cleaned_data['show_sup']
              show_coor = form.cleaned_data['show_coor']
              glow_project="Projects"+show_name
              pro_drive_path=Production_Drive+":"+"\\"
              wor_drive_path = work_drive + ":" + "\\"
              #text=form.cleaned_data['post']
              print("Production_Drive:",Production_Drive,"Work Drive:",work_drive,show_name,show_sup,show_coor,pro_drive_path,wor_drive_path)
      
          return render(request,'my_drive.html',{"form": form})
      

      【讨论】:

        猜你喜欢
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 2014-07-26
        • 1970-01-01
        • 2015-07-16
        相关资源
        最近更新 更多