3đź‘Ť
template
$(document).ready(function(){
$('.btn-success').on('click',function(){
$.get( "{% url 'execute' 'your_param' %}", function( data ) {
alert( "Done." );
});
});
});
url
urlpatterns += [
url(r'^execute/(?P<command>[a-z]+)',views.execute, name='execute'),
]
view
def execute(request,command):
if command == 'command1':
# do job 1
elif command == 'command2':
# do job 2
return Response("What you want to get on client as response")
4đź‘Ť
You are tackling a somewhat advanced Django topic as a newbie, but it should be doable. Note this is a non-Ajax python-only solution, not sure it’s exactly what you’re looking for, but it would be a common pattern for a Django project.
Create a view (connected to a URL) that will fire off an external python script and display a message. The view could be as simple as:
def fire_job(request):
exec(open('/path/to/external/script.py').read())
return render(request, 'some_template.html', {})
The template can say e.g. “Job has started. Please watch your email for updates.” Create a URL for the view and link to it from your nav.
That will basically work, but with one big problem: Python is synchronous by default so the user will not see the page until the job has completed.
Aside: During development, try calling a dummy script that will execute quickly but maybe includes something like:
import time
time.sleep(10) # 10 second artificial delay
Later, when everything is working, replace the path to your dummy script with the real thing.
Now you need a queueing system that will let you fire off a process in the background but return the page immediately, and that will send an email when the job is complete. A common solution for this is to use Celery, but I prefer Django_Q because it’s simpler to implement, with fewer moving parts.
Here is probably the easiest way to get it done with Django_Q (and while running ./manage.py qcluster
in a separate terminal):
def myview(request):
from django_q.tasks import async, result
task_id = async(
'appname.tasks.fire_job', # dotted path to function
hook='appname.tasks.report' # Runs when task completes
)
return render(request, 'some_template.html', {})
You now need to create those two functions: appname.tasks.fire_job
and appname.tasks.report
, changing the paths to match your project. The first one will be a simple function that calls your external script:
# appname/tasks.py
def fire_job():
exec(open('/path/to/external/script.py').read())
And another that gets fired when that job completes (e.g to send email):
# appname/tasks.py
def report():
# See Django docs on how to send email
That’s a bit simplified, but should get you going. Good luck!
- [Django]-Error on django runserver – OverflowError: getsockaddrarg: port must be 0-65535
- [Django]-Deleting the tags that are not used by any other object
- [Django]-Dynamic SEO-friendly URLs
- [Django]-ElasticSearch term suggest on analyzed field returns no suggestions
2đź‘Ť
This should look like this:
import jenkins
def hello(request):
if request.method == 'POST' and request.is_ajax():
server = jenkins.Jenkins('http://localhost:8080', username='myuser', password='mypassword')
server.build_job('api-test', {'param1': 'test value 1', 'param2': 'test value 2'})
last_build_number = server.get_job_info('api-test')['lastCompletedBuild']['number']
build_info = server.get_build_info('api-test', last_build_number)
return JsonResponse('info': build_info)
- [Django]-Django objects.get()
- [Django]-Missing mysql.sock; yielding OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
- [Django]-How to add the auto primary key into django's admin list_display
- [Django]-Adding javascript to an extended django template for google analytics