python function with django

0

Good morning, I'm starting to use Django and well I have a problem, and I'm not able to call a function that I created in python through a button and / or a href.

In my views.py file I have the method that I want to execute when I click on the link or on the button (try to do it in two ways and none of them work for me.-.- ').

@userRegistered
def getSyncGit(request, section):
       print 'PATATA' #(lo tengo para ver en el log si se ejecuta o no.)
       path = os.environ.get('BASE_DIR') + '/DEAdministration/admSyncGit/'
       cmd =  '. ' + path + 'p.sh '
       p = sp.Popen(['/bin/bash', '-c', cmd], stdout=sp.PIPE, stderr=sp.PIPE)
       result = p.wait()
       return HttpResponseRedirect(getURL(request.LANGUAGE_CODE, '/assistant/configuration/project/list/'))

Then in the file of urls.py I import this method and I create the url in the following way:

from .views import getSyncGit

url(r'^/project/sync/$', getSyncGit, {'section':'configuracion'}, name='pgetSyncGit'),

and then in the .html I have the following:

<script type="text/javascript">
    function sendSyncProject()
        {
        $.ajax({url: "{% url 'pgetSyncGit' %}", success: function(result){
            alert('bien');
            }});
        }
</script>
<td>
    <input id="butSendSyncProject" type="button" name="butSendSyncProject" style="margin-left:1px;" value="{% trans 'Sincronizar proyectos' %}" onclick="sendSyncProject()" />
</td>
<td>
    <a href = "{% url 'pgetSyncGit' %}"> asdasdasdasdasddas </a>
</td>

When I make the call by the button, I get the alert message, but the function is not executed, since it does not press the 'PATATA'

When I make the call through the href, it redirects me to the "/ project / sync /" page with an error, but neither does the method correctly because it does not do the print or anything ....

Any help?

Thank you very much.

--- EDITO --- At the time of executing it, I have open the server log, so I print them if they appear in the logger.

The strange thing is that ... If the method that I have created, I put it in an existing one, if it makes the call correctly and I do the print for the server log.

However, when I do it the way I'm doing it, by calling the url.py address with the method and passing the href to that address, I get the following error:

"Failed to delete table with cod_project sync. -- invalid literal for int() with base 10: 'sync'"

- EDIT2 - SOLVED.

I've just changed the order url.py ... and it works: S

    
asked by Iván Rodas Padilla 28.12.2017 в 17:27
source

1 answer

0

Good, the operation you describe seems normal, when you run your development server with the command python manage.py runserver you are only running the server, and you will not see in that window the print you make. You have to find another way to validate these things in django.

Because of what you say, you get to show the html, and also to execute the javascript, so the function will be executing it.

Take a look at the loggin module link

    
answered by 02.01.2018 в 10:37