1

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>`
4
  • is this using the default django templates? looks like the url block is being interpreted as a string instead. Commented Aug 21, 2014 at 3:48
  • @karthikr Yes, as far as I know, I'm using default templates. Commented Aug 21, 2014 at 3:51
  • have you set a base url meta tag? Commented Aug 21, 2014 at 4:05
  • @karthikr I have not. I'll look into that. Commented Aug 21, 2014 at 4:18

2 Answers 2

1

If the template code you've posted is an exact cut and paste from the actual file, you have a line break in the middle of the URL tag, between data_set.id and data_item.id. Django uses a fairly simple parser that can't recognise tags split over multiple lines, so that isn't being parsed as a tag at all.

Remove the line break and all should be well.

Sign up to request clarification or add additional context in comments.

3 Comments

I had really hoped that would work, but it didn't. I've checked to make sure my regex was correct, and it seems to be. I'm running out of ideas.
Never mind, I just had to restart my server. Thanks for the tip!
lol, this comment saved me from losing my mind!
0

Your template is not rendering correctly. Check your template code. The link being generated contains % signs.

2 Comments

I realize that, I'm just not sure what's wrong with the template code, as I'm doing things according to the Django documentation. At least as far as I know. :)
Post your full template code and the HTML source from a browser loading the page that contains the links

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.