发布表格问题后如何解决空白页?

发布于 2025-02-08 10:48:21 字数 1154 浏览 1 评论 0原文

我想向视图发送基本表格。我创建了所需的所有内容,但是当我提交帖子时,它不会发送返回空白页。 这是我的表单和应用程序:报告是与表单相同的页面,因为我想返回同一页面。我需要Year_one和Year_two的值。

<form  method="post" action="{% url 'app:reports' %}">
    {% csrf_token %}
    <label for="year_one">Select year 1:</label>
    <select id="year_one" name="year_one">
        {% for case in query_trend %}
            <option value="{{case.date_created__year}}"  >{{case.date_created__year}}</option>
        {% endfor %}
    </select>

    <label for="year_two">Select year 2:</label>
    <select id="year_two" name="year_two">
        {% for case in query_trend %}
            <option value="{{case.date_created__year}}">{{case.date_created__year}}</option>
        {% endfor %}
    </select>
    <button onclick="test()">click</button>
</form>

这是我的观点,

if self.request.method == 'POST':
            year_one = self.request.GET.get('year_one')
            year_two = self.request.GET.get('year_two')
            return HttpResponseRedirect('fdm:outstanding_reports')

在视图中使用这些值该怎么办?

I want to send a basic form to views. I create everything that it need but when I submit the post, it doesn't send a return a blank page.
This is my form and app:reports is the same page with form, because I want to return the same page. I need the values of year_one and year_two.

<form  method="post" action="{% url 'app:reports' %}">
    {% csrf_token %}
    <label for="year_one">Select year 1:</label>
    <select id="year_one" name="year_one">
        {% for case in query_trend %}
            <option value="{{case.date_created__year}}"  >{{case.date_created__year}}</option>
        {% endfor %}
    </select>

    <label for="year_two">Select year 2:</label>
    <select id="year_two" name="year_two">
        {% for case in query_trend %}
            <option value="{{case.date_created__year}}">{{case.date_created__year}}</option>
        {% endfor %}
    </select>
    <button onclick="test()">click</button>
</form>

And this is my view

if self.request.method == 'POST':
            year_one = self.request.GET.get('year_one')
            year_two = self.request.GET.get('year_two')
            return HttpResponseRedirect('fdm:outstanding_reports')

What should I do for using these values in views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2025-02-15 10:48:21

假设您的按钮的test()方法成功地提交了表单...如果您将请求传递给视图,即,

def reports(request):

您可以参考earl_one_one的张贴值和earth_two的张贴值,为:(

    if request.method == 'POST':
        year_one = request.POST.get('year_one')
        year_two = request.POST.get('year_two')

get是在发送中包含的值时URL,例如?ear_1 = 1975&amp; year_2 = 1300,帖子是在后台发生的使用小写'.get()'检索帖子值)

Assuming your button's test() method is successfully submitting the form...If you are passing the request to your view,ie,

def reports(request):

Then you can refer to the posted values of year_one and year_two as:

    if request.method == 'POST':
        year_one = request.POST.get('year_one')
        year_two = request.POST.get('year_two')

(GET is when you send values contained in the URL, eg page?year_1=1975&year_2=1300, POST is when it happens in the background. A form will generally use only one or the other, so you your version probably wasn't getting any values. Confusingly, you also use the lowercase '.get()' to retrieve the POST values)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文