Python Django获取用户输入 [英] Python Django Getting user input

查看:64
本文介绍了Python Django获取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个简单的html页面,该页面捕获用户输入的信息,并根据用户输入的信息创建一个新页面.问题是我无法找回用户在被支持者那里输入的信息,而且我也不知道我要去哪里错了.

I am setting up a simple html page, the page captures the information that the user entered and based on the information that the user entered makes a new page. The problem is that I cant get back the information entered by the user at the backed and I dont understand where I am going wrong.

我的视图文件的设置如下:

My views file is setup like this:

def suggestion(request):  
    if request.method == 'POST':
        form = BusinessName(request.POST)
        if form.is_valid():
            data=form.cleaned_data  
            context = insert_function_here(data)
            return render( request,'mainpage.html', context)
    else:
        form = BusinessName()  
        context = {'form':form}
        return render( request,'mainpage.html', context) 

我的forms.py的设置如下:

My forms.py is setup like this:

class BusinessName(forms.Form):
    business_name = forms.CharField(widget = forms.HiddenInput(), required = False)

我html的相关部分设置如下:

The relevant part of my html is set up like this:

<form id="user_input_form" method="post" action="http://127.0.0.1:8000/textinsighters/suggestion">
    Enter Your Business Name : <input type="text" list="browsers" name="browser" id="user_input">
    {% csrf_token %}
    {{ form }}
      <datalist id="browsers">
        <option value="Internet Explorer">
        <option value="Firefox">
        <option value="Chrome">
        <option value="Opera">
        <option value="Safari">
      </datalist>
      <button onclick="myFunction()">Submittt</button>
</form>
<script>
    function myFunction() {
document.getElementById("id_business_name").value = document.getElementById("user_input").value;                                            
document.getElementById("user_input_form").submit();
    }
</script>

我想要一个自动完成列表,这就是为什么我要用html创建表单的原因.我得到用户输入,将Django表单字段的值设置为用户输入的值并提交.我应该找回一些东西,但是视图中的变量数据"不包含用户输入.

I want an auto-completing list so thats why I am creating a form in html. I get the user input, set the value of the Django form field to the value that the user entered and submit it. I should get something back but the variable 'data' in views doesnt contain the user input.

谢谢

推荐答案

您正在使用forms.HiddenInput()作为窗口小部件,然后自己添加form字段.这样是行不通的.如果您将字段类更改为TextInput,该怎么办:

You are using a forms.HiddenInput() as the widget and then add the form field yourself. This doesn't work that way. What if you change the field class to TextInput:

class BusinessName(forms.Form):
    business_name = forms.CharField(widget = forms.TextInput())

如果您的目标是向窗口小部件添加自定义属性,则可以通过提供attrs字典来实现:

If you're goal is to add custom attributes to the widget, then this can be done by providing an attrs dictionary:

class BusinessName(forms.Form):
    business_name = forms.CharField(widget = forms.TextInput(attrs={
        'list': 'browser'
    }))

或者您可以查看 django-widget-tweaks 包在模板中添加属性.

Or you could have a look at the django-widget-tweaks package to add attributes in the template.

这篇关于Python Django获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆