{"id":773,"date":"2022-08-30T15:06:56","date_gmt":"2022-08-30T15:06:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/finding-it-hard-to-use-a-href-template-tag-in-django-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:56","modified_gmt":"2022-08-30T15:06:56","slug":"finding-it-hard-to-use-a-href-template-tag-in-django-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/finding-it-hard-to-use-a-href-template-tag-in-django-collection-of-common-programming-errors\/","title":{"rendered":"Finding it hard to use a href template tag in Django-Collection of common programming errors"},"content":{"rendered":"<p>I seem to have some difficulty trying to use a href template tag. Here is what I want to do.<\/p>\n<p>I have an edit order form at<\/p>\n<pre><code>(r'^orders\/edit\/(?P\\d+)\/$', views.edit_order),\n<\/code><\/pre>\n<p>I want a link that could take me to this html template items.html I will name this url<\/p>\n<pre><code>(r'^orders\/edit\/add_items\/(?P)\/$', views.add_items),\n<\/code><\/pre>\n<p>And my views looks like this.<\/p>\n<pre><code>@login_required\ndef add_items(request, client_id = 0):\n    client = None\n    items = None\n    try:\n        client = models.Client.objects.get(pk = client_id)\n        items = client.storageitem_set.all()\n    except:\n        return HttpResponse(reverse(return_clients))\n    return render_to_response('items.html', {'items':items, 'client':client}, context_instance = RequestContext(request))\n\n@login_required\ndef return_clients(request):\n    clients = models.Client.objects.all()\n    return render_to_response('clients.html', {'clients':clients}, context_instance = RequestContext(request))\n<\/code><\/pre>\n<p>In my edit order form template I have this.<\/p>\n<pre><code>Add Item\n<\/code><\/pre>\n<p>Now when I restart my server, I click on the link and I go for some reason to this page.<\/p>\n<p>\/orders\/edit\/add_items\/\/<\/p>\n<p>I think maybe I need a &#8216;for&#8217; loop, but for some reason when I tried to do this, the href disappeared.<\/p>\n<p><strong>EDIT:<\/strong> I have put in views.edit_order<\/p>\n<pre><code>client = models.Client.objects.all()\n<\/code><\/pre>\n<p>and in edit_order template<\/p>\n<pre><code>{% for c in client %}\n        {{c.name}}<br \/>\n{% endfor %}\n<\/code><\/pre>\n<p>While running server again, this displays all clients names but like before all clients links goes again to \/orders\/edit\/add_items\/\/<\/p>\n<ol>\n<li>\n<p>Found out the answer myself.<\/p>\n<pre><code>def edit_order(request, order_no):\n# some code \n    name = order.contact.client\n\n\nAdd Item\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Looks like <code>client<\/code> library at you case is undefined (I assume that HTML you have shown is located in clients.html)<\/p>\n<p>Try this:<\/p>\n<pre><code>{% for client in clients %}\n    Add Item<br \/>\n{% endfor %}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:41:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;^orders\/edit\/(?P\\d+)\/$&#8217;, views.edit_order), I want a link that could take me to this html template items.html I will name this url (r&#8217;^orders\/edit\/add_items\/(?P)\/$&#8217;, views.add_items), And my views looks like this. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-773","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}