Django button GET request with ajax

Viewed 41

Its my button :

            <form action="{% url 'panel:lights' %}" method='GET'>
            {% csrf_token %}
            <button class="btn" type='submit' onclick=mybtn() >ON</button>
            </form>

its my urls.py :

app_name = 'panel'
urlpatterns = [
    path('panel', views.panel_views , name = 'panel'),
    path('light', views.light_views , name = 'lights'),

its my views.py :

def light_views(request):
        print ("finally worked")
        Device.objects.all()
        device = Device.objects.get(id=1)
        device.status = not device.status
        device.save()
        return redirect(request , 'panel/panel.html')

and the error i get when i click button is :

Reverse for '<WSGIRequest: GET '/panel/lightcsrfmiddlewaretoken=eIQ9rKiLaTvmOqMtXAoi9J1YcuOgEEtuaAlkV67hMk8iYZsXlEahyd7E62g5eXWu'>' not found. 
'<WSGIRequest: GET '/panel/light?csrfmiddlewaretoken=eIQ9rKiLaTvmOqMtXAoi9J1YcuOgEEtuaAlkV67hMk8iYZsXlEahyd7E62g5eXWu'>' is not a valid view function or pattern name.

and the ajax code i used and it seems its wrong is :

<script> 
function mybtn() {
  var xhttp;
  if (window.XMLHttpRequest) {

    xhttp = new XMLHttpRequest();
  } else {

    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhttp.open("GET","{% url 'panel:light' %}", true);
  xhttp.send();
};
</script>
0 Answers
Related