1

I think this is probably something very simple, but I can't figure out for the life of me why these urls aren't matching.

My template code looks like this:

<form action="{% url 'view_record' "facility_report" %}" method="post">
{% csrf_token %}

{{ form }}

<input type="submit" value="View Report" name='view' label="Submit">  </form>

The url is then supposed to match this line in my url conf:

url(r'^view_record/((?P<report_type>.+)/)?$', views.view_record, name='view_record'),

What am I missing here? They simply won't match and most of the other questions regarding this are from five years ago when the engine seems to have been a lot more picky with formatting.

Exception Type: NoReverseMatch at /view_record/
Exception Value: Reverse for 'view_record' with arguments '('facility_report',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['view_record/((?P<report_type>(.*))/)?$']
6
  • can you show your views.py file? Commented Aug 19, 2015 at 15:08
  • What does the traceback say? Commented Aug 19, 2015 at 15:13
  • It's a reverse url matching error, couldn't match '('facility_record',)' after trying the posted url. I'll put up the exact log and view momentarily. Commented Aug 19, 2015 at 15:20
  • Ok, added the error message. The view is actually quite large and is unrelated to the error as far as I know. Commented Aug 19, 2015 at 15:42
  • can you tell what arguements are you passing in views? Commented Aug 19, 2015 at 15:44

2 Answers 2

1

The outer group in ((?P<report_type>.+)/)? is a capturing group. Django's url reversal can't handle nested capturing groups, so it will only catch the outer group as a possible argument. Since the first argument does not end in a /, the pattern doesn't match and a NoReverseMatch is thrown.

You can change the outer group to a non-capturing group, and Django will pick up the inner group as a capturing group. That way, the argument does not have to contain the /, as only the inner group is replaced, and the outer group is used as-is.

To create a non-capturing group, start the group with ?::

url(r'^view_record/(?:(?P<report_type>.+)/)?$', views.view_record, name='view_record'),
Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this:

<form action="{%url 'view_record' 'facility_report'%}" method="post">

And in your urls.py:

url(r'^view_record/(?P<report_type>(.*))/$', views.view_record, name='view_record')

This should send the form to the correct url.

3 Comments

I'm trying to avoid hard coding the url if possible.
Updated the answer. The problem was probably due to quotes. Hopefully this should solve your problem.
It keeps giving me the same error as before. This appears to be the correct format though, I'm really stumped as to why it isn't matching.

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.