Reputation: 753
I am trying to submit this form to my view:
in pcq_select.html
<form action="{% url 'pcq_list' product_id %}" method="POST">{% csrf_token %}
<label>Select the Product:
<select name="product_id">
{% for entry in products %}
<option value="{{ entry.id }}">{{ entry.productname }}</option>
{% endfor %}
</select>
<input type="submit" value="Go"></label>
</form>
in views.py
def pcq_select(request, template_name='maps/pcq/pcq_select.html'):
product = Product.objects.all()
return render(request, template_name, {'products': product})
def pcq_list(request, product_id="1"):
pcq = Pcq.objects.filter(product_id=product_id)
data = {}
data['object_list'] = pcq
return render(request, 'maps/pcq/pcq_list.html', data)
in urls.py
url(r'^pcq/list/(\d+)/$', views.pcq_list, name='pcq_list'),
I get the following error:
Exception Type: NoReverseMatch Exception Value: Reverse for 'pcq_list' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['maps/pcq/list/(\d+)/$']
Error during template rendering
In template E:\SampleSite\templates\maps\pcq\pcq_select.html, error at line 1
But when I replace the product_id in the action url with a number (example 1), the whole thing works good. Kindly help.
Upvotes: 2
Views: 28876
Reputation: 11192
The correct syntax is something like this:
{% for p in products %} <!-- Let's assume there are several products ... -->
...
<form action="{% url 'pcq_list' product_id=p.pk %}" method="POST">{% csrf_token %}
...
{% endfor %}
Then, you have to specify in your urls.py that the url is waiting for an id this way:
url(r'^pcq/list/(?P<product_id>\d+)$', views.pcq_list, name='pcq_list')
You can not write product_id
as is, since the template does not know this variable.
Upvotes: 4
Reputation: 8539
Are you passing a product to the template? If so, change your original product_id
to product.id
.
I.e.:
<form action="{% url 'pcq_list' product.id %}" method="POST">{% csrf_token %}
Upvotes: -1
Reputation: 600059
You don't show your view, but it looks like you are not passing anything called product_id
to the template.
Upvotes: 1