Easy way to submit POST data from a hyperlink in Django? Equivalent of Rails' `button_to`? - django

Is there a quick way to submit (pre-defined) POST data from a hyperlink in a Django template? I've got an 'add this to my favourites' link on page. I'm currently doing this with a GET request, which obviously breaks all kinds of rules.
I could manually build a form and have the link submit it with Javascript. I'm looking for an automatic way to do the same thing, essentially the equivalent of Rails' button_to.

Not that I can find. Sounds like a good candidate for a template tag and sharing.

Related

How can I implement 'select all' button in Django?

I'd like to implement select all button (e.g select all and deleting) in Django 1.9.7 but I can't find any post about it in Google Since, I'm not a non-native-english person, I'm not good at googling in correct english. Could anybody give me advices or keywords so that I can googling based on it?
Well this depends on how you implemented your table with Django. Putting javascript/bootstrap/etc aside, there are some samples on how you can implement select all in javascript. Like this.
Deleting those items requires defining a form and processing it's data in the serverside and deleting your model items. This also depends on how you are passing data between your view and your model.

Django how to link to some part of a page

I want a link in my webpage header bar that points to a certain part of a page (for example, the lower half). It's easily done with pure HTML but I wonder how to do it in django.
HTML version:
In base.html:
Introduction
In entrance.html:
<a name="intro"></a>
How can I do the same thing in django? Any better solutions than just writing one more view function?
There is no difference in doing it with Django when compared to just normal HTML. Just continue doing it the HTML way.
There's also another way to tag html elements by id AND use the url template tag from django:
https://engineertodeveloper.com/a-better-way-to-route-back-to-a-section-ids-in-django/
(Credits for Brian, who wrote the solution at the link above)
this can be very useful if you, for some reason, need to use the url method for creating your links.

Django form with autocomplete functionality

I am working on a django project with complex forms. In one of my form fields I need following functionality...... Its the text field. As the user starts typing the value the suggestions from existing database should appear in dropdown. Can anyone help me out with this ? Just similar to autocomplete but able to add new values.
This is going to be something in the JQuery/AJAX side of things, not Django. I would read up on the autocomplete functions of JQuery and use AJAX to call your DJango code and receive a populated list, which then displays to the user.
JQuery Autocomplete - Custom Data
If you don't want to deal with JavaScript, you can use a django application called django-autocomplete-light.
You can learn more about it (and get it) here: https://github.com/yourlabs/django-autocomplete-light

Django strategy for automatically suggesting matching content

I'm looking at porting a custom-written PHP CMS into Django. One of the features the CMS currently has is an image upload function. I write an article, tag it with information, then choose a photo for it. If the system has any photos which have been added to articles with tags in common with the new one, it will suggest the photo for that article too. If there are no matches then a new image can be added.
In case this doesn't make sense, let's say I tag an article as Bruce Springsteen, The Beatles and Led Zeppelin. Next time I add an article with the tag The Beatles, it should suggest I use the image added for the first article.
What would be the best Django-applicable way to implement this? I've looked at the Photologue app and have integrated it, and I know it has tagging support (the problem here is that I'm using django-taggit, whereas Photologue supports django-tagging). One approach could be simply building it myself -- when a user uploads an article, I run a hook after they save it to associate the tags with the image. I'm just not sure how to then autosuggest an image in the admin tools based on that info.
Any ideas/approaches greatly appreciated.
This is almost certainly something you're going to have to build yourself. Django has a moderate number of libraries out there (that you've clearly already found). Unlike other solutions, it doesn't have a lot of things that get you 100% to your desired solution (whereas something like Drupal might get you 100% of the way there).
What you will probably need to do (at a high level) is something like this:
Create an AJAX view that takes the current tags as an argument and does a query on the existing posts to see what tags match and returns images from those posts.
Use jQuery/javascript on your view to call your AJAX view on the page as tags are added
Use jQuery to update a <div> on your page and show all the images that your view returned
Here is a similar example that might help get you started.
You might look into django-ajax as a helper library for your requests, but it definitely isn't necessary.
The hook between the your image module and any other django module can be implemented using django's contenttypes framework which also provides some useful instance methods for returning related/hooked objects.

django - the best way to combine pagination with filtering and request.POST - like stackoverflow - ajax?

I want to combine pagination with filtering. Since I have a lot of filters I do not want to send them per GET request, since the URLs get really ugly.
Since django pagination uses GET request to pass the page parameters, I do not know how I can combine these two approaches.
Any idea?
Great add-on would be: How can I combine this approach with table sort? :-)
Edit:
Actually it should work like the pagination of stackoverflow - user questions. If a user clicks on a page number one is shown the correct page, without showing the get parameters in the url.
This is the url called.
https://stackoverflow.com/api/userquestions.html?page=2&pagesize=10&userId=237690&sort=Recent
But the url shown in the browser is neat and short.
Seems to be ajax. Anybody an idea how to implement this? :)
If the URL is not shown in the browser`s address bar, I do not care about whether it is beautiful or not.
Edit: The solution:
Make an ajax update with all filter parameters passed to the view. This should help you get started with implementing ajax for your site: link
Thus the GET parameters never show up in the address bar.
have you checked the paginate application for django?
it may help you a lot, use it all the time :D
http://code.google.com/p/django-pagination/
Have you considered django-tables2? It gives you django-admin style tables without you having to write the logic yourself.
maybe you can use the urs, something like:
http://oursite.com/something/filter1/filter2/3/
the doc -> http://docs.djangoproject.com/en/1.1/topics/http/urls/
I figured out two solutions:
Instead of using just hyperlinks use it inside a POST form, i dont have any example now but i remember have used that for REST functions in Ruby on rails
Save the query info in a session.
Hope this help.