开发者

Finding it hard to use a href template tag in Django

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<order_no>\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<client_id>)/$', 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.

<a href="{% url tiptop.views.add_items client.pk %}">Add Item</a>

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 %}
        <a href="{% url tiptop.views.add_items client.pk %}">{{c.name}}</a><br />
{% 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


<a href="{% url tiptop.views.client_items name.pk %}">Add Item</a>


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 %}
    <a href="{% url tiptop.views.add_items client.pk %}">Add Item</a><br />
{% endfor %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜