I am new absolutely new to Django and have been stuck on this problem for some time now, for some reason they don't actually explicitly teach it in any tutorial I could find.
So what I want is to create a simple button I Django that calls a simple python function that prints "hello" to the terminal? I don't want to change view or anything, just to call the function
EDIT:
This is what i have so far, left side is my view.py and right side is the html file
CodePudding user response:
Here is what you are looking for:
<form action="#" method="get">
<button type="submit" value="" name="mybtn">Download</button>
</form>
def index(request):
print('***Loaded***')
if (request.GET.get('mybtn')):
print('**Just Clicked on BTN')
CodePudding user response:
In Django workflow is different, It follows model view template (MVT), So you have to define a function in view.py in which you will mention your HTML template file_name, then you have to bind that function with your web app's URL using urls.py file, so when ever that particular URL will hits, it call views function process your logic and return data in your HTML file.
view.py
def welcome(request):
# here 'welcome.html' is your template it should be in templates directory
return render(request,'welcome.html')
urls.py
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('home', views.welcome, name = 'welcome'),
]
I suggest you to read its official docs, or you can go through this django crash course.

