none
Python Tools For Visual Studio, Django: How to properly pass values from a form in a template to a python function in views.py file

    Question

  • I've been trying to get values passed from a form in a template passed to a python function in my views.py file and executing that function with a button click in another template but I can't seem to get the python function to execute. How would I go about making it so that the python function actually executes instead of just redirecting back to the same page

    This is the form in one of my templates

    <div class="parameters">
    
    <body>
    <div class="boxed">
    <header>Tracking Parameters</header>
    <form id='Parameters' method="post">
    	{% csrf_token %}
    
    	Min Area: <br> <input type="text" name="MinArea"><br>
    	Max Area: <br><input type="text" name="MaxArea" ><br>
    	Field3:  <br><input type="text" name="Field3"><br>
    	Field4: <br> <input type="text" name="Field4"><br>
    	Field5: <br> <input type="text" name="Field5"><br>
    	<button type="submit" form="Parameters" value="Submit">Save</button>
    </form>

    Views.py function for the python code and all the dependencies

    from django.shortcuts import render
    from django.http import HttpRequest
    from django.template import RequestContext
    from datetime import datetime
    from django.http import HttpResponseRedirect
    from django.contrib import auth
    from django.template.context_processors import csrf
    from django.shortcuts import render_to_response
    from django.contrib.auth.forms import UserCreationForm
    from django.contrib.auth.decorators import login_required
    from django.contrib.auth import logout
    from django.contrib.auth.models import User
    import numpy as np
    import cv2
    def RunPythonCode(request):
        
        MHI_DURATION = request.POST.get("MinArea")
        MAX_TIME_DELTA = request.POST.get("MaxArea")
        MIN_TIME_DELTA = request.POST.get("Field3")
        filename = "C:/Users/ishraq/Downloads/traffic2.mp4"
        vidcap = cv2.VideoCapture(filename)
        success, frame = vidcap.read()
     
        h, w = frame.shape[:2]
        prev_frame = frame.copy()
        motion_history = np.zeros((h, w), np.float32)
        hsv = np.zeros((h, w, 3), np.uint8)
        hsv[:,:,1] = 255
     
        while(success):
             ret, frame = vidcap.read()
             frame_diff = cv2.absdiff(frame, prev_frame)
             gray_diff = cv2.cvtColor(frame_diff, cv2.COLOR_BGR2GRAY)
             ret, motion_mask = cv2.threshold(gray_diff, 127, 255, cv2.THRESH_BINARY)
             timestamp = clock()
             mhi = cv2.motempl.updateMotionHistory(motion_mask, motion_history, timestamp, MHI_DURATION)
             mg_mask, mg_orient = cv2.motempl.calcMotionGradient( motion_history, MAX_TIME_DELTA, MIN_TIME_DELTA, apertureSize=5 )
             seg_mask, seg_bounds = cv2.motempl.segmentMotion(motion_history, timestamp, MAX_TIME_DELTA)
     
             cv2.imshow('Trajectory', mhi)
     
             prev_frame = frame.copy()
             if cv2.waitKey(1) & 0xFF == ord('q'):
                break
             cv2.destroyAllWindows()
     
            
        return render_to_response('app/Configuration.html')
    url configuration for the python views function
    url(r'^RunPythonCode',views.RunPythonCode,name='runpythoncode')

    this is the button to call the url associated with the view function to execute the python code

    <button><a class="btn btn-default" href="/RunPythonCode" role="button" >Run Code</a></button>

    I have gotten python code to work with Django before but I was using notepad++ before for everything. Any help would be appreciated in solving this problem.

    Thanks in advance!




    Sunday, March 19, 2017 8:53 PM

All replies

  • Hi lshiii101,

    Thank you for your update.

    Refer to your description, this issue is about the development issue of Python project. Since our forum is to discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor, please redirect to this appropriate forum: https://github.com/Microsoft/PTVS/issues and start a new issue to seek for the professional support for your issue, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 21, 2017 2:05 AM
    Moderator