收集的 Django 表单数据未显示在包含 HTML 视图的目标 URL 中

问题描述 投票:0回答:1

我创建了一个 Django 表单,要求用户输入他的订单以在其他页面中显示它,但是当我单击提交按钮时,它会将我重定向到我想要的 URL,而不显示用户在表单中输入的数据。

这是view.py代码:

from django.shortcuts import render, HttpResponseRedirect
from django import forms 

# Create your views here.

orders_stock = ['headphones', 'earbuds', 'diy tools']



class NewOrder(forms.Form):
    get_order = forms.CharField(label='Your order', max_length=50)


def form(request):
    if request.method == 'POST':
        item = NewOrder(request.POST)
        if item.is_valid():
            get_order = item.cleaned_data['get_order']
            orders_stock.append(get_order)
            return HttpResponseRedirect('form/orders.html')
        else:
            return render(request, 'form/form.html', {
                'items': orders_stock
            })

    return render(request, 'form/form.html', {
        'form': NewOrder()
    })


def order(request):
    return render(request, 'form/orders.html', {
        'items': orders_stock,
    })

这是form.html:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form</title>
</head>
<body>
    <h2>Form submission</h2>
    <form action="{% url 'formsubmit:order'%}" method="post">
        {% csrf_token %}
        {{form}}
        <input type="submit" value="Submit">
    </form>
</body>
</html>

这是处理添加订单显示的orders.html代码:

<!DOCTYPE html>


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Orders</title>
</head>
<body>
    <h2>Orders</h2>
    <ul>
        {% for order in items %}
            <li>{{order}}</li>
        {% endfor %}
    </ul>
</body>
</html>

这是 urls.py 代码:

from . import views
from django.urls import path


app_name = 'formsubmit'

urlpatterns = [
    path('', views.form, name='form'),
    path('order', views.order, name='order')
]

我尝试了很多解决方案,但似乎没有一个能正常工作。

python forms django-views django-forms backend
1个回答
0
投票

您的代码存在一些问题,导致其无法按预期工作。这是需要修改的地方。

1。重定向到正确的 URL: 成功提交表单后,

HttpResponseRedirect
应重定向到
order
视图。但是,代码中的 URL
form/orders.html
不是 URL 名称。相反,请使用
HttpResponseRedirect(reverse('formsubmit:order'))

2。

form.html
模板中的表单操作:在 HTML 模板中,
action="{% url 'formsubmit:order' %}"
URL 指向订单显示页面,而它实际上应该指向表单提交路径 (
formsubmit:form
)。这样,
POST
数据就会进入处理表单提交的正确视图。

这是修改后的代码。

  • views.py
from django.shortcuts import render, HttpResponseRedirect
from django import forms 

# Create your views here.

orders_stock = ['headphones', 'earbuds', 'diy tools']



class NewOrder(forms.Form):
    get_order = forms.CharField(label='Your order', max_length=50)


def form(request):
    if request.method == 'POST':
        item = NewOrder(request.POST)
        if item.is_valid():
            get_order = item.cleaned_data['get_order']
            orders_stock.append(get_order)
            
            # Redirect to the 'order' view after successful submission
            return HttpResponseRedirect(reverse('formsubmit:order'))
        else:
            return render(request, 'form/form.html', {
                'items': orders_stock
            })

    return render(request, 'form/form.html', {
        'form': NewOrder()
    })


def order(request):
    return render(request, 'form/orders.html', {
        'items': orders_stock,
    })
  • form.html
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form</title>
</head>
<body>
    <h2>Form submission</h2>
    <form action="{% url 'formsubmit:form' %}" method="post">
        {% csrf_token %}
        {{form}}
        <input type="submit" value="Submit">
    </form>
</body>
</html>

希望这对你有一点帮助。

© www.soinside.com 2019 - 2024. All rights reserved.