I have this bit of code:
{% for i in list %}
<button>{{i}}</button>
{% endfor %}
How do I redirect to a new page and pass it value of i as a context, so that it will be like this:
<h3> You chose {{i}} </h3>
It doesn't have to be a button, in fact I'd prefer if it could be <span> with sort of the same functionality as in AngularJS ng-click
Do I need to send a request with button click and then handle it in views.py? Can I do this without a form?
I would prefer if it just redirected me to .../page?i='whatever'
I can do it with
<span>Click</span>
But what's the preferred Django way of doing it?
Do I need to send a request with button click and then handle it in
views.py?
Yes , you would need to create a view to handle that request that will be of type GET and will have data i inside it.
Can I do this without a form?
Yes of course you can and hence your approach of making it a link rather than a button is better and is the way it should/is done in most of the cases. But there is nothing Djangoish in this approach , its just the way it should be done...
I would prefer if it just redirected me to .../page?i='whatever' I can
do it with
Go ahead and do this way its fine.
<span>Click</span>
How do I redirect to a new page and pass it value of i as a context, so that it will be like this <h3> You chose {{i}} </h3>:
def link(request):
i = request.GET.get('i',None)
render_to_response('your_html_template', {"i": i}, RequestContext(request))
Related
I'm new to Angular and I just put in place an i18n (2 languages) system for a website I am creating. Everything works properly but in order to switch from one language to another in my header, I feel stuck.
I followed the Angular documentation to transfer my variables from child to parent component and I ended with this:
<input type="text" id="item-input" #lang>
<button type="button" (click)="changeChosenLang(lang.value)">
{{ 'global.lang' | translate }}
</button>
As you can see, I write my language in the input form and I send it to the proper component with a button. What I wanted was to click on my 'global.lang' text and to be able to send its value to the parent component, since the value is the language which is not actually used.
I don't know how to put my 'global.lang' text in a variable, neither what kind of balise I can use. Also I didn't know how to summarize my problem to search for it on StackOverflow so if you know a similar post, don't hesitate to post the link.
Thank you for your reading!
I found a less tortured way (poor brain) to have the result I wanted:
<span (click)="changeChosenLang()">
{{ 'global.lang' | translate }}
</span>
First I temporary changed my button to a span balise and I deleted the parameter from my changeChosenLang() function. Then, I transferred a variable 'lang' from my parent component to this one, witch contains the value of the language chosen in my app constructor. At each click, I change its value in my changeChosenLang() function and everything works great!
I hope it can help someone someday. The moral of this post is: the simpler, the better! Have a good day.
I am very new to Django and trying to make my first project. I find it difficult to use the buttons I created to move to the selected URL.
Let's say my app is called TestForms and my models are: Patients, General, PST and ERBT. I would like to create two buttons - 'Previous' and 'Next' - which will be used to go to previous/next forms respectively. I try to do so using admin templates in django.
NOTE: I know changing built-in templates are not a very good idea, I will create new html file to extend this templates before doing changes on the server. For now I am doing it locally on my computer.
In submit_line.html I created two new buttons and they are like so:
{% if show_save_and_go_to_next_form %}<input type="submit" value="{% translate 'Next' %}" class="default" name="_gotonextform">{% endif %}
{% if show_save_and_go_to_previous_form %}<input type="submit" value="{% translate 'Previous' %}" class="default" name="_gotopreviousform">{% endif %}
This gives me two good-looking buttons on the site.
But these are just saving the results (working like 'Save' button), but not redirecting me to the next form as I would like to. When I am adding a new patient (admin/TestForms/patient/add/), after clicking on 'Next' I would like the server to save this patient and redirect me to admin/TestForms/general/add/ to be able to fullfil the next form, then save the changes and move on to admin/TestForms/PST/add/and so on.
I know I have to add the anchor, but I tried multiple times with different approaches and nothing worked. When I try to use <a href ...>, the button disappears. Also it is difficult for me to figure out how to move from one form to another and to disable the 'Previous' button on the first form and the 'Next' button on the last form.
Any suggestions how to achieve it?
The redirect needs to be done in your view, not in the template.
def your_view(request, *args, **kwargs):
# your code ...
if request.POST.get('_gotonextform'):
return redirect('admin/TestForms/general/add/')
else:
# do whatever you like if any other button was clicked
pass
I am displaying a list of blog posts in a for loop, each has a comment form with send button underneath it. I need to let Django know which specific post has been clicked on so have added {{post.id}} to the button element's id. How do I then pass this information to jQuery? Is it possible to do something like this?
<button id="addComBtn{{post.id}}...>Send</button>
$('#addComBtn //post.id here').click(function() {
...
});
Or is there a better way?
Why haven't you tried out?
Yes it is possible. This should be the solution
<button id="addComBtn{{post.id}}...>Send</button>
$('#addComBtn{{ post.id }}').click(function() {
...
});
Because first Django renders the page, with its own engine(meaning, it doesn't check anything else outside of {%%}s and {{}}s), then it sends to the client, and when it renders, it will replace that.
I'm struggling to understand why and when you would make a form "hidden" in a Django app.
For example, when would you place a form in your template as
{{ form.as_hidden }}
and what is the practical difference between that, and placing it in a hidden <div>, like
<div style="display: none;">
{{ form.as_hidden }}
</div>
?
Django doesn't render entire forms as hidden so {{ form.as_hidden }} will not render anything.
Now if you want to talk about when to render fields of forms as_hidden, then we're in business.
Why you want to render fields as hidden inputs (it's what formfield.as_hidden does) is when you want to send a value to the client, store and get it back to your view since they are sent back to the server when the form is submitted.
I'm reading a Django tutorial and in the tutorial, the urls.py is this:
(r'^vote/$', bookmark_vote_page),
and there is a model called 'SharedBookmark':
class SharedBookmark(models.Model):
bookmark = models.ForeignKey(Bookmark, unique=True)
votes = models.IntegerField(default=1)
users_voted = models.ManyToManyField(User)
but in the template, the link which leads to /vote/ is this:
{% if shared_bookmarks %}
<ul class="bookmarks">
{% for shared_bookmark in shared_bookmarks %}
<li>
[+]
The view which handles the link is this:
#login_required
def bookmark_vote_page(request):
if request.GET.has_key('id'): #if it is a GET request
try:
id = request.GET['id']
shared_bookmark = SharedBookmark.objects.get(id=id) #if the bookmark is found
shared_bookmark.votes += 1 #make a change to the 'votes' field in the DB
shared_bookmark.users_voted.add(request.user) #make a change in the 'users_voted' field in the DB
shared_bookmark.save()
As you can see, the template appends '?id=x' (where x is a number) to the end of the URL and the view uses the GET request and makes a change to the database. From what I read, I should ONLY use POST requests if I want to modify the database. Is there a way for me to send a POST request rather than a GET request Without creating an entire HTML form / submit button?
You are right, you should use a post request if you want to change data on the server.
If you're just using html, then you need to create a form and a submit button. If you are using javascript, you could add a click handler to the link, which submits the form.
Ui