python – Error: Reverse for 'results' with arguments '('',)' not found. 1 pattern(s) tried: ['results/$']-ThrowExceptions

Exception or error:

My objective is to take two strings of user input, an origin and a destination, as a form. I want to render a form to the user where they populate these fields, then hit submit. The submit button should redirect the user to a ‘results’ page where the two inputted locations are printed with their full address on display, and the distance between the two points is displayed calculated via their gps coordinates. I keep getting this error message however, and I believe it is because I haven’t initialize the ‘distance’ properly in the index function in my views. I could be wrong, however.

I pasted my code below for reference. Please help me out, this is due really soon. All help is appreciated.

def index(request):
    distance = DistanceForm(request.POST or None)
    if distance.is_valid():
        distance.save()
        distance = DistanceForm()

    return render(request, 'distance/index.html', {'distance': distance})

def results(request, distance_id):
    distance = Distance.objects.get(id=distance_id)
    Distance.get_address_info(distance)
    Distance.get_distance(distance)
    return render(request, 'distance/results.html', {'distance': distance})
from django.urls import path
from . import views

app_name = 'distance'
urlpatterns = [
    # ex: /distance/
    path('', views.index, name='index'),
    path('results/', views.results, name='results')
]
from .models import Distance
from django import forms

class DistanceForm(forms.ModelForm):
    class Meta:
        model = Distance
        fields = [
            'origin',
            'destination'
        ]
<form action="{% url 'distance:results' distance.id %}" method="post">
    {% csrf_token %}
    {{ distance.as_p }} 

    <input type="submit">
</form>

enter image description here

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *