使用 Django 的 {% url %} 模板标签时,我得到了意想不到的结果。我正在寻找一个网址,例如:
domain.com/create_placement/<id>
而是返回以下内容:
domain.com/organizations/<id>/%7B%%20url%20'create_placement'%20org_id=organization.id%20%%3E
我的代码:
模板.py
...
<a href="{% url 'create_placement' organization.id %>">Create Placement</a>
...
视图.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})
网址.py
url(r'^create_placement/(?P<org_id>\d+)$', views.create_placement, name="create_placement"),