Finding it hard to use a href template tag in Django-Collection of common programming errors
I seem to have some difficulty trying to use a href template tag. Here is what I want to do.
I have an edit order form at
(r'^orders/edit/(?P\d+)/$', views.edit_order),
I want a link that could take me to this html template items.html I will name this url
(r'^orders/edit/add_items/(?P)/$', views.add_items),
And my views looks like this.
@login_required
def add_items(request, client_id = 0):
client = None
items = None
try:
client = models.Client.objects.get(pk = client_id)
items = client.storageitem_set.all()
except:
return HttpResponse(reverse(return_clients))
return render_to_response('items.html', {'items':items, 'client':client}, context_instance = RequestContext(request))
@login_required
def return_clients(request):
clients = models.Client.objects.all()
return render_to_response('clients.html', {'clients':clients}, context_instance = RequestContext(request))
In my edit order form template I have this.
Add Item
Now when I restart my server, I click on the link and I go for some reason to this page.
/orders/edit/add_items//
I think maybe I need a ‘for’ loop, but for some reason when I tried to do this, the href disappeared.
EDIT: I have put in views.edit_order
client = models.Client.objects.all()
and in edit_order template
{% for c in client %}
{{c.name}}
{% endfor %}
While running server again, this displays all clients names but like before all clients links goes again to /orders/edit/add_items//
-
Found out the answer myself.
def edit_order(request, order_no): # some code name = order.contact.client Add Item
-
Looks like
client
library at you case is undefined (I assume that HTML you have shown is located in clients.html)Try this:
{% for client in clients %} Add Item
{% endfor %}
Originally posted 2013-11-09 22:41:45.