2

I am running into a few issues regarding URL mappings in Django. I have the following code:

table.html:

<form id="filter_form" method="post" action="update_filters/">
    <input type="submit" name="submit" value="Report" />
</form>

urls.py:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^update_filters/', views.filter_report, name='update_filters'),
]

views.py:

def filter_report(request):
    # Code in the function
    return render(request, 'autotester/table.html', context)

and everything works, but when I hit the "Report" button multiple times I get:

127.0.0.1:8000/autotester/update_filters
127.0.0.1:8000/autotester/update_filters/update_filters
127.0.0.1:8000/autotester/update_filters/update_filters/update_filters  
etc

and I have no idea what's causing it. There has to be some sort of simple fix for this but I just can't find it and I have been trying to figure this out for 3 hours now and my brain is just fried.

steveclark
  • 537
  • 9
  • 27

1 Answers1

4

Try using {% url 'update_filters' %} template tag. And also add $ at the end of the regular expression in your url definition.

url(r'^update_filters/$', views.filter_report, name='update_filters'),
karthikr
  • 97,368
  • 26
  • 197
  • 188
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • That did it. Do you know why that fixes it? Or can you point me towards a reference that explains it? I just want to understand. – steveclark May 21 '15 at 19:39
  • 1
    The `$` symbol means the URL have to end there, and does not allow to append anything after. – Gocht May 21 '15 at 19:42
  • Oh well that to, but I meant why `{% url 'update_filters' %}` stopped my problem. I had the `$` there before, but when I hit the _Report_ button more than once it would throw an error saying it didn't know where _autotester/update_filters/update_filters_ was – steveclark May 21 '15 at 19:44
  • Why is here: If you write `
    ` it should work. You were missing `/` at the beginning, instead `{% url 'update_filters' %}` will print it in the rigth way.
    – Gocht May 21 '15 at 20:10