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.
Related
I am building a web-app using flask where I am trying to route some URLs
I have three routes with me
a) "/"
b) "/teacher"
c) "/student"
The routing works fine for "/" and"/student" The URL gets appended after clicking the submit button for student and"/student" gets appended in URL but on clicking the button associated with "/teacher" the URL doesn't gets appended and "/" page keeps on loading up.
Please help me out here and tell me what wrong I am doing
My Python code:
#app.route('/', methods=['GET', 'POST'])
def select():
return render_template("select.html")
#app.route('/teacher', methods=['GET', 'POST'])
def index():
return render_template("index_teacher.html", title="Faculty login")
#app.route('/student', methods=['GET', 'POST'])
def index1():
return render_template("index_student.html", title="Student login")
Part of my html code from select.html:
<form action="/teacher" method = "post">
<input type="submit" class="submit" value="Login as Teacher">
</form>
<br>
<form action="/student" method="post">
<input type="submit" class ="submit" value="Login as Student">
</form>
One more thing about the code, when I add a text box above the first submit button, both the button starts working and give desired results.
Thanks in advance!
I think you have just a little typo in the html. The input tag of teacher didn't close >.
<form action="/teacher" method = "post">
<input type="submit" class="submit" value="Login as Teacher">
</form>
<br>
<form action="/student" method="post">
<input type="submit" class ="submit" value="Login as Student">
</form>
EDIT:
In the case that the question was copied incorrectly, I don't know what should be wrong with your code. For me your sample code works just fine. So from my experience with flask and html forms the most common errors I made are the following, maybe it's one of them (the list may serve other too, so I will also add points that do not make sense for your case):
method name of two routes collide (e.g. both '/' and '/teacher' are assigned to function index, but that should give an error when starting the server, so not really possible)
an html tag is not closed properly, so the form in question is not parsed as a valid form (that was my guess with above answer, also makes sense with the text box, which may close the corrupt tag => in html an open tag is closed by the first following closing tag)
typo in the form action attribute (check the network tab in your browsers developer tools, maybe you get an error response to something like "taecher", which results in a redirect to "/", again not really plausible with the fact that it suddenly works when you add the text box)
typo in the route or the returned template (again not plausible with explained behavior with the added text box)
That's all I can think of at the moment. I hope it helps with the search. if you could add more of your html code, maybe I find something suspicious.
I am creating my first Django app, and I am trying to open a template using a button tag. I have something like this here:
<button type="submit" onClick="window.location.href='details'">View</button>
In my urls.py, I have:
url(r'^details/$', TemplateView.as_view(template_name='details.html'), name=""),
I am not sure how to set the URL in urls.py to be able to access the details.html page. Currently, the page does not open. Does someone know how I can open it please?
If you have type="submit", sounds like you are using a form, right? If you do have a form, your url shouldn't be on the button but on the form action attribute.
You need to give your url a name, like:
url(r'^details/$', TemplateView.as_view(template_name='details.html'), name="details"),
Then your action would be:
action="{% url 'details' %}"
Read some resource online to learn how to use a form action.
I suggest you learn some html basics before jumping into django development, it would save your time wondering these kind of questions.
If you are not using a form, you might just need an <a> tag that looks like a button with css decoration. If you are using bootstrap, check out .btn class in the documentation.
It would be better to avoid javascript and just make a really simple form:
<form method="GET" action="{% url 'details_page' %}">
<button type="submit">View</button>
</form>
But you also need to name the route like so:
url(r'^details/$', TemplateView.as_view(template_name='details.html'), name="details_page")
Links are still preferrable though. It would be better to use some sort of CSS to make links and buttons look the same so you can use them interchangeably like bootstrap.
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
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.
I'm using django-registration to handle login and user registration in my Django app. I'd like the user to get redirected to the url they requested after they have successfully logged on to the system (assuming they were logged off when they tried to access the url initially).
The login url looks like:
http://localhost:8000/accounts/login/?next=/#foo/bar/
However, after the login, the user always ends up at http://localhost:8000/. I've tried it with http://localhost:8000/accounts/login/?next=/abc/def/ and that worked fine, so I assume the hashtag is the problem. How can this be fixed (if at all)?
Update
I should have mentioned that it's a single page app, hence the hashtag...
I started stepping through the django-registration (actually django) code. I couldn't find anything django-registration is doing during the login, so I think it isn't even responsible for this part.
Django wraps #login_required view functions in _wrapped_view(request, *args, **kwargs) functions (django.contrib.auth.decorators) and performs an authentication test before executing the actual function. If found not logged in yet, it gets the current url as next (path = request.build_absolute_uri()) and starts the login. However, request.build_absolute_uri() doesn't even return the hashtag url. Extracting the #-appended part of the url does generally not seem to be possible.
Sorry for the necromancer, but I solved this issue with the following:
I have a template for the login form with the following:
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<input type="hidden" id="hidden_next" name="next" value="{{ next }}">
... the actual form here ...
</form>
and (I am using JQuery) the following script at the end:
<script>$(function () {
$("#hidden_next").attr(
"value", "{{ next }}" +
$(location).attr('hash'))
});
</script>
Note that I am repeating the {{ next }} just in case the user disables Javascript. In a single page app this is not an issue, as a user disabling javascript cannot use the page at all.