Error: NoReverseMatch at / Reverse for 'submit' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried:[u'$submit'/]

400 views
Skip to first unread message

Robbie Sharma

unread,
Jan 4, 2016, 7:16:32 AM1/4/16
to Django users

I have python and django loaded on my RaspberryPi that is wired to an LED array. The web app 'iofcontrol' in my Django project is supposed to activate an animation sequence of a set of LEDs.


  • iofppysite is the main project file
  • iofppysite/iofcontrol is the LED control web app!


There is a form with 3 radio buttons (Laser 1, Laser 2, Laser 3) and 2 submit buttons (Acquire, Deactivate). 


The radio buttons are linked to 1 name/variable, and the submit buttons have 2 different actions. When you enter the site, you can select only one "Laser" radio button and click "Acquire". It is supposed to perform an LED animation based on Laser 1. You click "Deactivate" to turn the LEDs off.


The LED code works by itself but I am having lots of issues with the POST, redirect, and HttpResponse logic. I can't seem to set it up right.


I have been getting this NoReverseMatch error or I cannot properly process the POST data. I am new with Django so I have no idea what I'm doing. I have been combing through tons of tutorials and problem articles but just can't seem to get it right.


Can someone help me figure this out and make it work for me?? You will totally make my day!


The error I am getting now is:




NoReverseMatch at /  Reverse for 'submit' with arguments '()' and keyword arguments '{}' not found. 0     pattern(s) tried:[u'$submit'/]

And the following is highlighted in my index file:

<form class="form-mini" method="post" action="{% url 'iofcontrol:submit' %}">


My code is below:



iofppysite/iofppysite/urls.py


from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [

    url(r'^admin/', admin.site.urls),
    url(r'^', include('iofcontrol.urls', namespace='iofcontrol')),
]


iofppysite/iofcontrol/urls.py


from django.conf.urls import url
from . import views

urlpatterns = [

    url(r'^', views.index, name='index'),
    url(r'^submit/', views.submit, name='submit'),
]



iofppysite/iofcontrol/index.html - Form portion only


<form class="form-mini" method="post" action="{% url 'iofcontrol:submit' %}">

        {% csrf_token %}




            <div class="form-row">
                <div class="form-radio-buttons">

        <div>
            <div>
                <label>
                    <span>Laser type</span>
                </label> 

                <label>
                    <span>Select one to acquire</span>
                </label>


                       </div>
            <div>
                <label>
                    <span>Laser 1</span>
                </label>

                            <label>
                                <input type="radio" name="laser_id" value="0" {% if laserAct == 0 %}    {{"checked"}}{% endif %}>

                            </label>

                        </div>

            <div>
                <label>
                    <span>Laser 2</span>
                </label>
                            <label>
                                <input type="radio" name="laser_id" value="1" {% if laserAct == 1 %}    {{"checked"}}{% endif %}>

                            </label>

                        </div>

            <div>
                <label>
                    <span>Laser 3</span>
                </label>
                            <label>
                                <input type="radio" name="laser_id" value="2" {% if laserAct == 2 %}    {{"checked"}}{% endif %}>

                            </label>

                        </div>





                </div>
            </div>

            <div class="form-row form-last-row">
                <button name="btnSubmit" type="submit" value="a">Acquire</button>
        <button name="btnSubmit" type="submit" value="s">Shutdown</button>
            </div>

        </form>



iofppysite/iofcontrol/views.py


from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import loader

from iofled import control

# For debugging purposes, python debugger
import time
import pdb

def index(request):
               context = {'led_message : "Index page access"}        
        print "A request was made: {0}".format(request)

        return render(request, 'iofcontrol/index.html', Context(context))

def submit(request, laser_id):

        try:
                oLED = IoFLED()
                btnSubmit = request.POST['btnSubmit']
                laserid = request.POST['laser_id']



                print "The values of b n l: {0} and {1}".format(btnSubmit, laserid)

                #If acquire was selected
                if(btnSubmit == "a" and laserid != False):

                        oLED.deactivateLEDs()
                        print "LED deactivated"
                        oLED.activateLEDWiFi()
                        print "WiFi activated"
                        oLED.activateLEDAnim(laserid)                                             
                        print "Animating LEDs"
                        context = {
                         'led_nessage': "Acquiring based on Laser %s" % laserid,
                         'laserAct': laserid,
                        }
                #If shutdown was selected
                elif(btnSubmit == "s"):
                        oLED.deactivateLEDs()
                        oLED.activateLEDWiFi()

                del oLED

        except Exception as e:
                if(oLED):
                        del oLED
                context = {
                         'error_message': "A proper selction was not made. %s" % e,

                }

                return render(request,'iofcontrol/index.html', context)
        else:
                return HttpResponseRedirect(reverse('iofcontrol:index', args = (context,)))




Víctor Suárez

unread,
Jan 4, 2016, 12:12:59 PM1/4/16
to Django users

Luis Zárate

unread,
Jan 4, 2016, 5:13:45 PM1/4/16
to django...@googlegroups.com


El lunes, 4 de enero de 2016, Víctor Suárez
urlpatterns = [

   url(r'^', views.index, name='index'),
   url(r'^submit/(?P<laser_id>\d+)', views.submit, name='submit'),
]


Because you have

def submit(request, laser_id):


<vesr.c...@gmail.com> escribió:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f93eaa09-f4b1-4cfc-af6a-0c2541524a47%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



James Schneider

unread,
Jan 5, 2016, 4:35:25 AM1/5/16
to django...@googlegroups.com


>
> The error I am getting now is:
>
>
>
>
> NoReverseMatch at /  Reverse for 'submit' with arguments '()' and keyword arguments '{}' not found. 0     pattern(s) tried:[u'$submit'/]
>
> And the following is highlighted in my index file:
>
> <form class="form-mini" method="post" action="{% url 'iofcontrol:submit' %}">
>
>
> My code is below:
>
>
>
> iofppysite/iofppysite/urls.py
>
>
> from django.conf.urls import include, url
> from django.contrib import admin
>
> urlpatterns = [
>
>     url(r'^admin/', admin.site.urls),
>     url(r'^', include('iofcontrol.urls', namespace='iofcontrol')),
> ]
>

Not sure what the other response was referring to, the namespace definition is there.

>
> iofppysite/iofcontrol/urls.py
>
>
> from django.conf.urls import url
> from . import views
>
> urlpatterns = [
>
>     url(r'^', views.index, name='index'),
>     url(r'^submit/', views.submit, name='submit'),
> ]
>

And now we've come to the brunt of the problem. Your 'index' URL has a bad regex. A simple r'^' will match every single URL in this namespace, because all you are looking for is the beginning of a string, whether it is empty or not. That renders the rest of the URL's in that namespace unusable, since they are matched top-down.

That also explains your error where the URL that matched is listed as '/$submit/' rather than just '/submit/'.

You probably want r'^$', which matches only an empty string (beginning and end of string are next to each other).

Your second URL also probably needs some help: r'submit/$'

If you don't do that, you may get unexpected behavior later if you have another overlapping URL defined like r'submit/thank_you/$', which your current 'submit' URL would match. You probably don't want that to happen.

It's important to be as specific as possible with regular expressions. Be sure to include a $ at the end of your URL regular expressions if that is exactly the URL you are matching. In general, you won't have them for definitions that include() other URL files, but they'll likely be on all of your other definitions.

Also remember the trailing / before the $ as well, assuming that you have ADD_SLASHES set to True (it is True by default).

-James

James Schneider

unread,
Jan 5, 2016, 4:48:35 AM1/5/16
to django...@googlegroups.com


> def submit(request, laser_id):
>
>         try:
>                 oLED = IoFLED()
>                 btnSubmit = request.POST['btnSubmit']
>                 laserid = request.POST['laser_id']
>
>

The other poster reminded me, you'll want to remove the laser_id reference in your submit() signature. You aren't capturing the laser_id in your urls.py, even with the changes I suggested. Loading this view as it sits will throw an exception. You appear to be pulling the necessary info from the POST dict, making it even more unnecessary.

If you do want to capture the ID as part of the URL, go with what the other poster suggested, but I don't believe that's your intention.

-James

Reply all
Reply to author
Forward
0 new messages