0

I am trying to make a form with an input button group that redirects the user to another URL given the button they pushed. I am currently receiving a NoReverseMatch when I don't think I should be.

I went through the top answer of What is a NoReverseMatch error, and how do I fix it? but don't think those apply to me.

The form in index.html:

    <form action="/main/community/" method="get">
    <div class="btn-group" role="group" aria-label="Basic example">
        <input type="button" class="btn btn-secondary" value='Comm1'>
        <input type="button" class="btn btn-secondary" value='Comm2'>
        <input type="button" class="btn btn-secondary" value='Comm3'>
    </div>
    </form>

My URL's:

app_name = 'main'
urlpatterns = [
    path('', views.Index, name='index'),
    path('community/', views.Community, name='community'),
    path('community/choice',views.CommView, name='CommView'),
    path('admin/', admin.site.urls)
]

My views:

def Community(request):
    try:
        pass
    except (KeyError):
        pass
    else:
        return HttpResponseRedirect(reverse('community/choice'))

def CommView(request):
    return HttpResponse("Test success!.")

When I press the buttons no redirect occurs. When I manually input the URL of /community/choice/ I receive the following error:

NoReverseMatch at /main/community/

Reverse for 'community/choice' not found. 'community/choice' is not a valid view function or pattern name.

Stoner
  • 846
  • 1
  • 10
  • 30
Joseph Rajchwald
  • 487
  • 5
  • 13
  • PS: I know my form isn't passing any information to then next URL at the moment - I'm just trying to test the redirect. – Joseph Rajchwald Sep 06 '19 at 01:13
  • Use the url _name_, not the actual _url pattern_, as the argument to `reverse()`. i.e. you want `reverse('commView')`. – John Gordon Sep 06 '19 at 01:13
  • @JohnGordon that didn't work either. I received the same error but this time it was: NoReverseMatch at /main/community/ Reverse for 'CommView' not found. 'CommView' is not a valid view function or pattern name. – Joseph Rajchwald Sep 06 '19 at 01:17

1 Answers1

0

I think you should try main:CommView . App_name:view_name like you define in urls.py

In my project i use like this

return reverse('blog:article_detail', kwargs={
            'article_id': self.id,
            'title': self.title
        })