0

All,

I have just started learning python and django and I was wondering if there's a way to call a python script (Test.py) from a link on the django page. So far I have created a django project (TestDjango) and I have these files in that folder:

__init__.py
settings.py
urls.py
views.py
Test.py

This is what I have in my views.py script so far:

from django.http import HttpResponse

def index(request):
return HttpResponse('''<h1>Test</h1> 
    <p>
    <!-- <a href = "https://www.python.org/"> Python </a> </br> -->
    </p>
    '''

and in my urls.py I am calling it this way:

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name ='index'),
]

When I run this on the server I get a simple page with title - "Test" and a clickable "Python" link which takes me to the python website. However, what I am looking for is a way to add a link on that django page which would call my "Test.py" script when you click on, say "Test". I have done some visualizations in that script with some mocked up data that I'd like to see on my webpage. It might be something very simple, but, I couldn't find a way to do it. Any guidance would be appreciated.

Please let me know if I missed something or any suggestion you have for me when I post any question next time.

3
  • You're going to need to have the site POST back to the server where you can handle the request and call the script. It's not exactly straight forward if you haven't done it before, but definitely possible Commented Mar 19, 2019 at 21:09
  • Here's a simple solution: Run your visualization code inside the index view function. If your visualization code outputs an image, you can then return the image in the HTTPResponse. Commented Mar 19, 2019 at 21:11
  • Thanks xyres. The Test.py script has 2 visualizations with a filter. I am a beginner, so, an example would be great. Really appreciate your help! Commented Mar 20, 2019 at 2:21

1 Answer 1

1

You almost never want to call python scripts directly in Django. In fact, the only python script that is generally running is wsgi.py. That calls functions and classes from all your other scripts. So whenever you want to do something like call a script when a user clicks on a link on your site, you create a view in one of your views.py files, then register that view in your urls.py file so that when the user clicks on the aforementioned url, they activate the view you wrote.

views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse('''
        <h1>Test</h1> 
        <p>
            <a href="https://www.python.org/"> Python </a> </br>
            <a href="/test/"> Python </a> </br>
        </p>
    ''')

def test(request):
    return HttpResponse("test")

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name ='index'),
    path('test/', views.test, name ='test'),
]

If you want to see the traceback inside the test view at some point, then do this:

def test(request):
    some_variable = 1
    raise Exception(some_variable)
    return HttpResponse("test")

Just remember to remove that exception before pushing live.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for looking in to this. But, on doing this, when I click on the new link "Test" it opens a page with just text as "Test". It doesn't really show me the contents inside that Test.py script. Thoughts?
Python scripts, just like php or any other language that I know of besides javascript, do not show up on the page when you render them. Only the result, the stuff inside HttpResponse() does. So for testing, if you want to be able to view some variable to diagnose what is happening in the script at any given point, either write return HttpResponse(some_variable) at that point, or just insert raise Exception(some_variable) to see the full traceback.
I've appended a bit of code to my answer to demonstrate that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.