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.
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.
This is a filter in my Django Templates tag.
#register.filter('saving_bookmarks_db')
def saving_bookmarks_db(news_data,news_source,):
#this will save the data in db
In my Django Template, i have one button like this
<button data-toggle="modal" id="myClickButton" href="#dbModal" class="btn btn-info pull-right custom" >Bookmark</button>
this is my include command, which will include modal html and save the also save the data using tag
{% include "db_saving.html" with source=source data=data %}
All i want to do is that, this include command execute only when the button is click but in django whenever the page is refresh, it save all the data in the DB and not the one when i click the button.
You cannot do things like this in a template filter. That will always be executed on render.
Anything that affects the database in response to user action can only happen in a view, accessed via a URL. It sounds like you want that to be called from an Ajax function in your template which in turn is called from your onclick handler.
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'm using django-voting: https://github.com/brosner/django-voting/tree/master/voting
And after my post, I'm redirected here: http://www.iana.org/domains/example/#c40
<form method="POST" action="/comments/{{ comment.id }}/up/vote/">
{% csrf_token %}
<button type="submit">Thumbs Up!</button>
</form>
The vote is created and I can see it in the admin.
No where in my application do I have this kind of redirect. Nor can I find this line of code in django-voting where it would have this redirect. Has this happened to anyone else, if so how'd you solve this?
I just want to be redirected the same page after the casted the vote. So I tried <input type="hidden" name="next" value="{{ event.get_absolute_url }}" /> thinking that it might override. But this doesn't seem to work. Suggestions?
I'm guessing that somewhere in your logic you redirect to "example.com". example.com is owned by the IANA and redirects to http://www.iana.org/domains/example/.
Also, the "#c40" at the end of the url makes me think that Django is trying to redirect back to some page with a c40 anchor, possibly in order to have the thing you just voted on in view.
Are you using the Sites framework? (Do you have a Sites model in your admin?) If yes, this may be due to having an instance of a Site model with the domain name "example.org" which is the default.
Furthermore, there may be a fixture that reloads 'example.org' into the Sites each time you do something with the database, such as a migration with South, for example.
That redirect happens when you hit one of the example domains, listed in RFC 2606.
There's something somewhere in your code that's pushing to example.com or another one of the example domains.
I'm trying to build a simple website with login functionality very similar to the one here on SO.
The user should be able to browse the site as an anonymous user and there will be a login link on every page. When clicking on the login link the user will be taken to the login form. After a successful login the user should be taken back to the page from where he clicked the login link in the first place.
I'm guessing that I have to somehow pass the url of the current page to the view that handles the login form but I can't really get it to work.
EDIT:
I figured it out. I linked to the login form by passing the current page as a GET parameter and then used 'next' to redirect to that page. Thanks!
EDIT 2:
My explanation did not seem to be clear so as requested here is my code:
Lets say we are on a page foo.html and we are not logged in. Now we would like to have a link on foo.html that links to login.html. There we can login and are then redirected back to foo.html.
The link on foo.html looks like this:
<a href='/login/?next={{ request.path }}'>Login</a>
Now I wrote a custom login view that looks somewhat like this:
def login_view(request):
redirect_to = request.REQUEST.get('next', '')
if request.method=='POST':
#create login form...
if valid login credentials have been entered:
return HttpResponseRedirect(redirect_to)
#...
return render_to_response('login.html', locals())
And the important line in login.html:
<form method="post" action="./?next={{ redirect_to }}">
So yeah thats pretty much it, hope that makes it clear.
You do not need to make an extra view for this, the functionality is already built in.
First each page with a login link needs to know the current path, and the easiest way is to add the request context preprosessor to settings.py (the 4 first are default), then the request object will be available in each request:
settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
Then add in the template you want the Login link:
base.html:
Login
This will add a GET argument to the login page that points back to the current page.
The login template can then be as simple as this:
registration/login.html:
{% block content %}
<form method="post" action="">
{{form.as_p}}
<input type="submit" value="Login">
</form>
{% endblock %}
To support full urls with param/values you'd need:
?next={{ request.get_full_path|urlencode }}
instead of just:
?next={{ request.path }}
This may not be a "best practice", but I've successfully used this before:
return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))
Django's built-in authentication works the way you want.
Their login pages include a next query string which is the page to return to after login.
Look at http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.decorators.login_required
I linked to the login form by passing the current page as a GET parameter and then used 'next' to redirect to that page. Thanks!
I encountered the same problem. This solution allows me to keep using the generic login view:
urlpatterns += patterns('django.views.generic.simple',
(r'^accounts/profile/$', 'redirect_to', {'url': 'generic_account_url'}),
)
In registration/login.html (nested within templates folder) if you insert the following line, the page will render like Django's original admin login page:
{% include "admin/login.html" %}
Note: The file should contain above lines only.
See django docs for views.login(), you supply a 'next' value (as a hidden field) on the input form to redirect to after a successful login.
You can also do this
<input type="hidden" name="text" value="{% url 'dashboard' %}" />