简体   繁体   中英

SIMPLE Django button to execute view function

After a few days of tutorials I am calling it quits, any help would be appreciated. Can't get this to work, I'm sure it's easy, but this stuff is extremely convoluted. Just trying to delete a session variable with a button. I tried Ajax examples as well. Please don't forward me to another post unless you are sure it will actually work, because many do not.

URL: urls.py
...
url(r'^$', clear_session, name='clear_session'),
...

and

View: views.py

def clear_session(request):

    print('hello')

    if not request.POST:

        print('hello')
        del request.session['jobs_append']

        print('session list', request.session['jobs_append'])

and

Template: index.html

...
<input id="clear_sesh" name="update_log" type="button" value="Update Log"/>
...

<script type="text/javascript">
$(function(){
$("#clear_sesh").click(function(){
    $.post(
     url : "/clear_session/",
     dataType:"html",
     success: function(data, status, xhr){
        //do something with your data
    }
    );
    return false;
});
});
</script>
...

    <!-- jQuery -->
<script src="{% static 'vendor/jquery/jquery.min.js' %}"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="{% static 'vendor/bootstrap/js/bootstrap.min.js' %}"></script>

<!-- Plugin JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="{% static 'vendor/scrollreveal/scrollreveal.min.js' %}"></script>
<script src="{% static 'vendor/magnific-popup/jquery.magnific-popup.min.js' %}"></script>

<!-- Theme JavaScript -->
<script src="{% static 'js/creative.min.js' %}"></script>

I can't see any issue with the code of the view, but your url code and your jQuery call isn't right, because you url for that view is empty, meaning that view is called in your first page (ex. 127.0.0.1:8000 ), but you are calling the url "/clear_session/" (ex. 127.0.0.1:8000/clear_session/ ) with AJAX, in summary you are never calling your view and by extend your code never is executed.

UPDATE

This is a working example that should help you to make your views. Also I found an issue with your AJAX call, you should be using a request type other than post because you're explicitly excluding post requests within your view. Now the example:

urls.py

url(r'^clear-session-url/$', clear_session, name='clear_session_name'),
url(r'^update-session-url/$', update_session, name='update_session_name')

views.py

from django.http import JsonResponse

def clear_session(request):
    if not request.POST:
        del request.session['jobs_append']
    return JsonResponse({'jobs_append': request.session.get('jobs_append', None)})


def update_session(request):
    if not request.POST:
        request.session['jobs_append'] = True
    return JsonResponse({'jobs_append': request.session.get('jobs_append', None)})

index.html

<script type="text/javascript">
    $.get('/update-session-url/', function(data) {console.log(data)});
    $.get('/clear-session-url/', function(data) {console.log(data)});
</script>

I added a second view to allow easy testing and to empathize how you should be using your urls. Also used different strings for all parameters to reduce the confusion you have with the url function. Note: you'll see the results of this calls in your browser's javascript console.

I got this to work without any script:

template: index.html

<input type="button" onclick="window.location.href='clear_session.html'" value="Clear Session">

and

view: views.py

def clear_sesh(request):

print('SESSION CLEARED')

request.session.pop("jobs_append", None)

return redirect('index')

and

URL: urls.py

url('clear_session', clear_sesh, name='clear_sesh')

and it now redirects to where I want. Do you see any issue with my solution? I think understanding that url patterns can't overlap was useful to me, so the redirect was seemingly golden.

Also, I am still new to SO, should I test your solution and mark your answer as successful/upvote?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM