I Get An Error. Django.urls.exceptions.noreversematch
l am started to learn Django few days ago, and I get this error: django.urls.exceptions.NoReverseMatch: Reverse for 'create_order' with no arguments not found. 1 pattern(s) tried:
Solution 1:
As the error said, it tried with empty argument, means there was no customer value available in context. So you need to send the customer
value through context, like this:
context = {
'customer' : customer,
'form': form
}
Solution 2:
I was also following this tutorial from youtube(dennis ivy) and got the same error, don't know what is the problem but just replace the file urls.py from github with the same context and it's not showing that error,.
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('products/', views.products, name='products'),
path('customer/<str:pk_test>/', views.customer, name="customer"),
path('create_order/<str:pk>/', views.createOrder, name="create_order"),
path('update_order/<str:pk>/', views.updateOrder, name="update_order"),
path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),
]
views.py
from django.forms import inlineformset_factory
defcreateOrder(request, pk):
OrderFormSet = inlineformset_factory(Customer, Order, fields=('product', 'status'), extra=10 )
customer = Customer.objects.get(id=pk)
formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
#form = OrderForm(initial={'customer':customer})if request.method == 'POST':
#print('Printing POST:', request.POST)#form = OrderForm(request.POST)
formset = OrderFormSet(request.POST, instance=customer)
if formset.is_valid():
formset.save()
return redirect('/')
context = {'form':formset}
return render(request, 'accounts/order_form.html', context)
order_form.html
{% extends'accounts/main.html' %}
{% load static %}
{% block content %}
<div class="row">
<divclass="col-md-6"><divclass="card card-body"><formaction=""method="POST">
{% csrf_token %}
{{ form.management_form }}
{% for field in form %}
{{field}}
<hr>
{% endfor %}
<inputtype="submit"name="Submit"></form></div></div></div>
{% endblock %}
again I don't know why it was showing this error and where was the problem but just relapced it with the same code from github and it worked..if someone know how it worked , that will be really helpful in near future. thanks to all regards Haris Ahmad
Post a Comment for "I Get An Error. Django.urls.exceptions.noreversematch"