I created a Django form that asks the user to enter his order to display it in other page, but when I click on submit button it redirects me to the the URL I want without displaying the data the user entered in the form.
This is the view.py code:
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,
})
This is the 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>
And this is the orders.html code which handle the added orders display:
<!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>
And this is the urls.py code :
from . import views
from django.urls import path
app_name = 'formsubmit'
urlpatterns = [
path('', views.form, name='form'),
path('order', views.order, name='order')
]
I've tried many solutions but nothing seems to work appropriately.
Your code has a few issues that are preventing it from working as expected. Here's the modification needed.
1. Redirect to the Correct URL: When the form is successfully submitted, the HttpResponseRedirect
should redirect to the order
view. However, the URL in your code, form/orders.html
, is not a URL name. Instead, use HttpResponseRedirect(reverse('formsubmit:order'))
.
2. Form Action in form.html
Template: In your HTML template, the action="{% url 'formsubmit:order' %}"
URL points to the order display page, while it should actually point to the form submission route (formsubmit:form
). This is so the POST
data goes to the correct view that processes the form submission.
Here's the modified code.
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,
})
<!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>
I hope this will help you a little.