简体   繁体   中英

Django url template tag returns encoded url

When using Django's {% url %} template tag, I am getting unexpected results. I am looking to get a url such as:

domain.com/create_placement/<id> 

instead the following is returned:

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


My Code:

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"),

Your problem is the ending %>

{% url 'create_placement' organization.id %>

should be

{% url 'create_placement' organization.id %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM