在Django中未获取表单的值 [英] Not getting the value of a form in Django

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

问题描述

在Django中,我想打开一个URL(由用户以表单的形式发布),然后单击带有URL的(表单的)提交按钮,然后获取数据(URL)并对其进行一些处理(在视图中打印)。

In Django I want to open a URL (this is posted by a user in a form) and after clicking the submit button (of the form) with the URL, get the data (the URL) and do some things with it (printing it in the view).

我在 views.py 文件中只有一个功能,所以我猜猜我的主要问题是我应该有两个。但是我不确定如何解决它,但是我已经尝试过了。

I only have one function in the views.py file, so I guess my main problem is that I should have two. But I am not really sure how to solve it, however have I tried.

我的表单是这样的:

<html>
    <head>
        <title>
            Practica Django
        </title>

    </head>

        <form method="POST" action="" id = "loginForm">
               {% csrf_token %}
            <input type="text" name="text" id= "text" value= '{% if submitbutton == "Submit" %} {{ firstname }} {% endif %}' maxlength="100"/>
            <input type="reset" name="Reset" id = "Reset" value="Reset" />
            <input type="Submit" name="Submit" id = "Submit" value="Submit" />

            {% if submitbutton == "Submit"  %}

            <h1 id = 4 name="resultado2"> {{ type }}</h1>
        {% endif %}

        </form>

</html>

在我看来,我有:

def vistaFormulario(request,):
    text = request.POST.get('text')
    submitbutton = request.POST.get('Submit')
    m = str(text)
    print(m)
    print(text)

    # Descarga el contenido del HTML
    with urllib.request.urlopen('http://www.elmundo.es/espana/2018/06/05/5b162c3b468aebd9678b4695.html') as response:
        page = response.read()
    soup = BeautifulSoup(page, 'html.parser')
    p = soup.find_all('p')

因此它继续。但是我需要打开该URL!但是无论用户使用表单中的哪种形式书写并按输入按钮!

And so it continues. But I need to open that URL! But whichever the user has written in the form and push the input button!

如您所见,我有两张印刷品,仅查看这些值具有哪些值。他们在输入内容并按下输入按钮之前没有任何内容,然后在按下输入按钮之后,便拥有了您所编写的内容。

As you can see, I have two prints, just to see what values do those values have. They have None before typing something and pressing the input button, and after pressing it, they have whatever you have written.

我想要的是获得这种方法而无论我如何从一开始在按钮中输入。我不明白为什么他们没有。我想获取值,然后打印出来。

What I want is to get in this method whatever I input in the button from the beginning. I do not understand why they have none. I want to get the value, and then print them.

推荐答案

所以,您的问题是您打电话给Beautiful Soup ,但是当用户最初加载页面且未提交表单时,您无需这样做。

So, your issue is that you do a call to Beautiful Soup, but you don't need to do it when the user initially loads the page and hasn't submitted the form.

最简单的解决方案是放置调用 bs 放入 if 块,该块仅在您 POST 时触发:

The easiest solution is to put the logic that calls bs into an if block that only fires if you POST:

# Define these so that they have a value to be called in the template
text = submitbutton = ''
if request.method == 'POST':
    text = request.POST.get('text')
    submitbutton = request.POST.get('Submit')
    m = str(text)
    print(m)
    print(text)

    # Descarga el contenido del HTML
    with urllib.request.urlopen('http://www.elmundo.es/espana/2018/06/05/5b162c3b468aebd9678b4695.html') as response:
       page = response.read()
   soup = BeautifulSoup(page, 'html.parser')
   p = soup.find_all('p')

# This needs to be outside your 'if' clause, so that the page will render if the method isn't POST
context = {'text': text,
           'submitbutton': submitbutton,
           'type':q1}

return render(request, 'form/form.html', context)

从您的样本中,我不确定 qt 的定义方式或确切原因您正在捕获提交按钮,但这应该或多或少地起作用。

From your sample, I'm not sure how qt gets defined or exactly why you're capturing the submitbutton, but this should work more or less.

这篇关于在Django中未获取表单的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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