I've found a few questions here related to my issue, but I haven't found anything that has helped me resolve my issue. I'm using Python 2.7.5 and Django 1.8.dev20140627143448.
I have a view that's interacting with my database to delete objects, and it takes two arguments in addition to a request:
def delete_data_item(request, dataclass_id, dataitem_id):
form = AddDataItemForm(request.POST)
data_set = get_object_or_404(DataClass, pk=dataclass_id)
context = {'data_set': data_set, 'form': form}
data_item = get_object_or_404(DataItem, pk=dataitem_id)
data_item.delete()
data_set.save()
return HttpResponseRedirect(reverse('detail',
args=(dataclass_id,)))
The URL in myapp.urls.py looks something like this:
url(r'^(?P<dataclass_id>[0-9]+)/(?P<dataitem_id>[0-9]+)/delete_data_item/$',
views.delete_data_item, name='delete_data_item')
and the portion of my template relevant to the view is:
<a href="{% url 'delete_data_item' data_set.id data_item.id %}">DELETE</a>
Whenever I click on the DELETE link, django tells me that the request URL:
http://127.0.0.1:8000/myapp/5/%7B%%20url%20'delete_data_item'%20data_set.id%20data_item.id%20%%7D
doesn't match any of my URL patterns. What am I missing? The URL on which the DELETE links exist is myapp/(<dataclass_id>[0-9]+)/
EDIT:
An additional detail that I should have included:
when I manually type in the correct URL (i.e.: myapp/3/62/delete_data_item), the delete of the item in the database and the call to reverse both work perfectly.
Full Template Code:
`<h1>{{ data_set.name }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<ul>
{% for data_item in data_set.dataitem_set.all %}
<li>{{ data_item.log_date }}: {{ data_item }}
<ul>
<li>{{ data_item.id }}</li>
<li><a href="{% url 'delete_data_item' data_set.id
data_item.id %}">DELETE</a>
</li>
</ul>
</li>
{% endfor %}
<form action="{% url 'add_data_item' data_set.id %}" method="post">
{% csrf_token %}
<li>{{ form.as_p }}</li>
<input type="submit" value="Add data item"/>
</form>
</ul>
HTML Code from Browser:
<h1>Hours Slept</h1>
<ul>
<li>Aug. 18, 2014, 9:10 p.m.: 8 hours
<ul>
<li>8</li>
<li><a href="{% url 'delete_data_item' data_set.id
data_item.id %}">
DELETE</a>
</li>
</ul>
</li>
<li>Aug. 18, 2014, 9:11 p.m.: 1 hours
<ul>
<li>10</li>
<li><a href="{% url 'delete_data_item' data_set.id
data_item.id %}">
DELETE</a>
</li>
</ul>
</li>
<li>Aug. 21, 2014, 3:13 a.m.: 2.5 hours
<ul>
<li>60</li>
<li><a href="{% url 'delete_data_item' data_set.id
data_item.id %}">
DELETE</a>
</li>
</ul>
</li>
<form action="/lets_quantify/5/add_data_item/" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='FoDLqsbDsuTGu1LBFv3lYZvD2UBa6oUz' />
<li><p><label for="id_amount">Amount:</label> <input id="id_amount" name="amount" step="0.00001" type="number" /></p></li>
<input type="submit" value="Add data item"/>
</form>
</ul>`