Django url模板标签返回编码的url

时间:2013-11-12 03:37:14

标签: python django url

使用Django的{%url%}模板标签时,我得到了意想不到的结果。 我希望得到一个网址,如:

domain.com/create_placement/<id> 

而是返回以下内容:

domain.com/organizations/<id>/%7B%%20url%20'create_placement'%20org_id=organization.id%20%%3E


我的代码:

template.py

...
<a href="{% url 'create_placement' organization.id %>">Create Placement</a>  
...


views.py

def create_placement(request, org_id):
    if request.method == "POST":
        organization = Organization.objects.get(id=org_id)

        ## Gather post data
        placement_form = PlacementForm(request.POST)

        ## Validate Form
        if placement_form.is_valid():
            ## Send to database
            placement_form.save()

            ## redirect to placement view
            placement = Placement.objects.latest('id')
            return redirect('individual_organization', id=placement.id)            

    else:
        placement_form = PlacementForm(initial = { "organization" : organization})

    return render(request, "admin_tracking/create_placement.html", {'placement_form': placement_form})

urls.py

url(r'^create_placement/(?P<org_id>\d+)$', views.create_placement, name="create_placement"),

1 个答案:

答案 0 :(得分:1)

您的问题是结尾%>

{% url 'create_placement' organization.id %>

应该是

{% url 'create_placement' organization.id %}