I am a novice Django user trying to create a push button, which links to another page in my site when clicked. I've tried a few different examples which I've found, but none seem to be working for me...
As an example, why is this not working?
<form method="link" action="{% url 'gui' %}">
<input type="button" value="Start">
</form>
Here, 'gui' is the name I have given to my link in urls.py to the desired page.
I am also fairly new to HTML in general, so it may be my HTML that is wrong...
Thank you.
Is there any particular reason you're using both a form and a button?
Can you use an a (anchor tag)?
Click here
If you want to use a form, please post your urls.py.
To create a button that can be clicked and redirect to another link, you can wrap <button></button> around <a></a>, which worked for me:
<button type="button">
Click here!
</button>
<form method="post" action="{% url 'gui' %}">
<button type='submit'>Start</button>
</form>
In your views:
def function(request):
if request.method == 'POST'
# Do something
Related
In my home.html template:
<button type="button" class="btn btn-dark"
method = 'GET' action = '/action_name/' name="audio_record">Record Audio</button>
In my views.py:
def audio_functions(request):
print('called function')
In my urls.py:
path('/action_name/', views.audio_functions, name='audio-record'),
what am i doing wrong?
Edit: I replaced the button with the suggested version below:
<a href="{% url 'audio-record' %}" class="btn btn-dark"
>Record Audio</a>
But I have a new problem. I actually don't want to redirect to by url/action_name. I just want to trigger the python script within the browser. How can I do this?
in html :
<a href="{% url 'audio-record' %}" class="btn btn-dark"
>Record Audio</a>
and urls.py
path('action_name', views.audio_functions, name='audio-record'),
In your urls.py you do not need the leading forward slash, as django adds this in automatically. Replace it with this and it should work:
path('action_name/', views.audio_functions, name='audio-record'),
Also the method and action attribues would normally go in the <form> tag, and not the button one. Also change type to submit on your button.
As #SALAHEDDINEELGHARBI says, you should really be using {% url 'audio-record' %} rather than hard-coding the url, however this is not the problem in this case (you shouldn't have a leading slash in urls as this would leave to a url with a double slash)
EDIT - In response to your edit:
You can't trigger a python script within the browser. It's a common misconception. Django is a web framework built in python, yes. But anything that happens in the browse has to happen in javascript. If you want to use python, you'll need to make a call to some django endpoint, do the python, and the send it back.
I was searching for this answer but none met my expectation. So, In my template I have some content and wanted to add button (which later will add to favorites). After clicking I want to call method from my views.py and redirect to other view.
my views.py
def home(request):
//logic here
request.session['url'] = url
return render(request,'file.html')
def function_to_call(request):
///logic here
url = request.session.get('url')
return render(request,'second_file.html',url=url)
file.html
<form action="{% url 'function_to_call' %}">
<button id="submit" type="button" value="Click" />
</form>
and in my urls.py
url(r'^function_to_call/',views.function_to_call,name='function_to_call'),
Unfortunately, after clicking on button, nothing happens
unless you are submitting a form, you should use
Click
If for some reason you need to use a POST request rather than a GET this will work:
<form method="POST" action="{% url 'function_to_call' %}">
<button id="submit" type="submit" value="Click" />
</form>
Using a post can be helpful when you don't want to include data in the querystring because it's a little less secure than having the parameters in the request's body.
I want to execute a python script after a user (registered user) clicks on a button in HTML using Django. How can this be achieved? Can someone give me a hint on how to create the logic in the views.py along with the Html code in the template? Before executing the button code, I want to check whether if the user.id exists in the model. If not, the button doesn't do anything.
Currently, this is basic understanding:
views.py:
def button(request):
#python script
return render(request, 'users/home.html')
home.html:
{% if user.id %}
<button class="btn btn-outline-info" onclick="location.href={% url 'button' %}"> Manual Index </button>
FYI, I am new to Django and am still exploring its potential.
Add your function in urls.py, example code is
from django.contrib import admin
from django.urls import path
from .views import button
urlpatterns = [
.....
path("button/", button, name="button_call"),
.....
]
and simply you can link your button for the url, example code is
{% if user.id %}
<button class="btn btn-outline-info" onclick="location.href={% url 'button_call' %}"> Manual Index </button>
more flexible way to do this is
<button class="btn btn-outline-info"> Manual Index </button>
happy coding
So I am trying to alter one of the forms from Django-registration: an app I installed via pip.
As stated in the docs, I was to created a registration/registration_form.html and use form as my context:
<html>
<p>This is the registration form</p>
<ul>
{{ form.as_ul}}
</ul>
</html>
So I have 2 questions:
1) How am I to alter the form to have a submit button that actually works in this case?
2) How can I alter the django-registration models so that I can ultimately add more to the registration forms?
Yes I looked over the docs, I am asking here because the language confused me and seemed slightly advance for me.
Thank you!
You need to add the form tags and a submit button. Something like this:
<html>
<p>This is the registration form</p>
<form action="/url/to/register/" method="POST">
{{form.as_ul}}
<input type="submit" value="Register">
</form>
</html>
where "/url/to/register/" will need to be pointed at your view code in your urls.py. Something like this:
from django.conf.urls import url, patterns
from yoursite.registrations import views
urlpatterns = patterns('',
url(r'^url/to/register/', views.register_some_guy),
)
I am a newbie in Django, though I code in python using Twisted, and I still have a long way to go dealing with this platform. Ihave a problem regarding the use of url templates in forms. I have defined a regex path
(url(r'^search/(?P<key>\w*)/$', views.searchKey, name='search'),)
in my urls.py. The url path works when I hard code the url path. For example:
"/search/potatoes/"
But when I use forms to post a search to that path, I always get "/search//". Below is my forms code. What seems to be happening?
<form action="{% url 'feeds:search' key %}" method="post">
{% csrf_token %}
<input type="text" name="key" id="key" />
<input type="submit" value="Filter" />
</form>
Thanks in advance!
You've set that as a keyword argument, so I think you'll need to use {% url 'feeds:search' key='potatoes' %} or if you have an actual variable there {% url 'feeds:search' key=key_variable %}.
Now if you're trying to get the form to submit to that URL you'll need to do some JavaScript on the frontend to get that URL changed based on what's input into the key field.