django:无法通过单击提交按钮来调用python函数

时间:2018-07-13 12:44:45

标签: python django

当我们单击HTML页面上的“提交”按钮并仅从HTML中获取数据时,尝试调用“安装”功能,这对django是陌生的。 帮帮我..!

查看文件:

from .forms import Details
from django import forms
from django.shortcuts import render
from django.http import HttpResponseRedirect
from . import installation

def output(request):
    if request.method == 'POST':
        details=Details(request.POST)
        if details.is_valid():
            Obj=details.cleaned_data
            path=Obj['Path']
            device_type=Obj['Device']
            image_version=Obj['Version']
            return installation()
    else:
        raise TypeError

App / urls.py文件:

from django.conf.urls import url
from django.contrib import admin
from . import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'',views.output,{'template_name':'home_page.html'}),
]

Forms.py文件:

from django import forms

class Details(forms.Form):
    Device=forms.CharField(required=True, label='Device_Type', max_length=32)
    Path=forms.FileField(required=True, label='Path_to_install_build')
    Version=forms.CharField(required=True, label='Version_type',max_length=32)

1 个答案:

答案 0 :(得分:0)

您的HTML在哪里?如果您的前端(HTML)在您的视图中未到达后端,则很可能您的操作不正确,因此请检查表单是否具有正确的操作

<form action="/" method="POST"> 
   {% csrf_token %} 
   <!-- Rest of the HTML code to your form -->
   <input type="submit">Submit</input>    
</form>

将您的操作设置为/,因为您使用了视图的根路径,如果将其更改为类似细节,则操作应为/detail/

顺便说一句,您可以使用Django中的内置表单

 {% csrf_token %} 
 {{ form.as_p }}
 <input type="submit">Submit</input>