Search code examples
pythondjangoweb-deployment

Reverse for 'edit_order' with arguments '(17,)' not found. 1 pattern(s) tried: ['order/<int:pk>/$']


I tired my best to solve this error but I couldnt solve this error. details errro is given: NoReverseMatch at /order/

Reverse for 'edit_order' with arguments '(17,)' not found. 1 pattern(s) tried: ['order/int:pk/$']

Request Method: POST Request URL: http://127.0.0.1:8000/order/ Django Version: 1.11.29 Exception Type: NoReverseMatch Exception Value:

Reverse for 'edit_order' with arguments '(17,)' not found. 1 pattern(s) tried: ['order/int:pk/$']

Exception Location: C:\Python27\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497 Python Executable: C:\Python27\python.exe Python Version: 2.7.16 Python Path:

['C:\Users\dipesh\Desktop\nandiasgraden-project\nandiasgarden', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages']

Server time: Wed, 8 Jul 2020 12:02:12 +0000

Url.py

from django.contrib import admin from django.conf.urls import url from pizza import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    url(r'^order/', views.order, name='order'),
    url(r'^pizzas/', views.pizzas, name='pizzas'),
    url(r'^order/<int:pk>/$', views.edit_order, name='edit_order'),
  ]

View.py

from django.shortcuts import render
from .forms import PizzaForm, MultiplePizzaForm
from django.forms import formset_factory
from .models import Pizza

def home(request):
    return render(request, 'pizza/home.html')

def order(request):
    multiple_form = MultiplePizzaForm()
if request.method == 'POST':
    filled_form = PizzaForm(request.POST)
    if filled_form.is_valid():
        created_pizza = filled_form.save()
        created_pizza_pk = created_pizza.id
        note = 'Thanks for ordering! Your %s %s and %s pizza is on its way!'  
         %(filled_form.cleaned_data['size'],                                                
        filled_form.cleaned_data['topping1'],
        filled_form.cleaned_data['topping2'],)
        new_form = PizzaForm()
        return render(request, 'pizza/order.html', {'created_pizza_pk':created_pizza_pk, 
        'pizzaform':new_form, 'note':note, 'multiple_form':multiple_form, })           
else:
    form = PizzaForm()
    return render(request, 'pizza/order.html', {'multiple_form':multiple_form, 
    'pizzaform':form})

def pizzas(request):
    number_of_pizzas = 2
    filled_multiple_pizza_form = MultiplePizzaForm(request.GET)
    if filled_multiple_pizza_form.is_valid():
       number_of_pizzas = filled_multiple_pizza_form.cleaned_data['number']
    PizzaFormSet = formset_factory(PizzaForm, extra=number_of_pizzas)
    formset = PizzaFormSet()
    if request.method == "POST":
       filled_formset = PizzaFormSet(request.POST)
       if(filled_formset.is_valid()):
          for form in filled_formset:
             print(form.cleaned_data['topping1'])
          note = 'Pizzas have been ordered!'
       else:
          note = 'Order was not created, please try again'
       return render(request, 'pizza/pizzas.html', {'note':note, 'formset':formset})
   else:
      return render(request, 'pizza/pizzas.html', {'formset':formset})
def edit_order(request, pk):
  pizza = Pizza.objects.get(pk=pk)
  form = PizzaForm(instance=pizza)
  if request.method == 'POST':
     filled_form = PizzaForm(request.POST,instance=pizza)
     if filled_form.is_valid():
        filled_form.save()
        form = filled_form
        note = 'Order has been updated.'
        return render(request, 'pizza/edit_order.html', 
         {'note':note,'pizzaform':form,'pizza':pizza})
 return render(request, 'pizza/edit_order.html', 
 {'pizzaform':form,'pizza':pizza})

order.html

<h1>Order a Pizza</h1>

<h2>{{ note }}</h2>
    {% if created_pizza_pk %}
    <a href="{% url 'edit_order' created_pizza_pk %}">Edit Your 
      Order</a>
    {% endif %}


<form action="{% url 'order' %}" method="post">
    {% csrf_token %}
    {{ pizzaform }}
    <input type="submit" value="Order Pizza">
 </form>

<br><br>
Want more than one pizza?
<form action="{% url 'pizzas' %}" method="get">
     {% csrf_token %}
     {{ multiple_form }}
     <input type="submit" value="Get Pizzas">
</form>

order_edit.html

<h1>Edit Order</h1>

<h2>{{ note }}</h2>

<form action="{% url 'edit_order' pizza.id %}" method="post">
{% csrf_token %}
{{ pizzaform }}
<input type="submit" value="Edit Order">

Solution

  • You are mixing up path with url. If you are using Django>=2.0, then try the following:

    from django.urls import include, path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', views.home, name='home'),
        path('order/', views.order, name='order'),
        path('pizzas/', views.pizzas, name='pizzas'),
        path('order/<int:pk>/', views.edit_order, name='edit_order'),
      ]
    

    If you are using Django<2.0, then try this:

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$', views.home, name='home'),
        url(r'^order/', views.order, name='order'),
        url(r'^pizzas/', views.pizzas, name='pizzas'),
        url(r'^order/(?P<pk>\d+)/$', views.edit_order, name='edit_order'),
      ]
    

    More information can be found in documentation.