Add other elements than fields in Django forms - django

I'm using Django to set up a website, where users can add spatial informations associated with ecological datasets. I'm using leafletjs to display pins on the map, where datasets have been sampled.
The solution I have now, is to have users write lat/lon as fields. On the other hand, there is an example on the leaflet website where clicking on the map will display the coordinates, and that is much more user friendly. So I'd like to have this + a javascript to fill the lat/lon position, in the form rendered by Django.
Is there any chance I can put that in a Django form, or must I write the form in HTML?

In this case, I would use an AJAX form to post entries in the back end, and upon submission recall the entries and write it out as JSON. Django has native functionality for all of this.
Here is a recent SO example: How to submit form without refreshing page using Django, Ajax, jQuery?

Related

How to build conversational form with django?

I am trying to build a conversational form with Django.
It will be used in landing page. The form questions will be loaded one by one as user answers them. And there will be some greeting and "human-way" responses to user input (such as "wow! you did a good choice!" after user selects one of the choices from form). The experience and look of the app will be like a real-time chat but user can only select one of the choices from form or upload a file/image.
1. Which technology is better to use for it? I am planning to do it with Fetch.
2. Since I want it to work without page reloading, how do I need to load Django forms through Fetch? Do I need to pass elements of it with JSON and construct it in client-side or can I just pass it as an html with {{form.as_p}} and display it in HTML?
Does these options make difference in matter of security?
I do not know anything about Fetch, but anyway I think it must be constructed clientside, but at first I would simply display the form in a template to get the ids of its fields and then use it in clientside code.
What about security - you'll need to pass the csrf token via your form.

Improving the loading performance of foreignkey fields with thousands of options

I have a similar situation as this post in which I encounter a slow page loading as I have thousands of entries in a foreignkey field.
At modelform, is there a way to improve the page loading while keeping the dropdown function? I have used select2 to efficiently find the chosen item in the dropdown, thus want to keep this function.
Django has to fetch all those foreignkey objects from database and then render them as HTML. This is why it takes a lot of time. Fetching from database can be pretty quick if you cache everything, but rendering to HTML will still be a problem.
Here's a solution that I think would work best:
Exclude that ForeignKey field from your form.
Instead, just create a blank input field. You'll have to capture user's input via JavaScript and send that value to your backed to fetch suggestions. In fact, select2 supports fetching data from backend via AJAX, So, half of your work is done.
Create a view which will take that AJAX request and search the database for suggestions and send them back to the client.
And you're done.

Ajax - How to save automatically my Django form

i have an add_form under my django app. i want to add a feature this form which is saving form automatically after user starts to type.
like in gmail , or blogger.
what are the steps i should follow? which way or plugin? and how to use them?
any knowlenge can help me.thank you.
There's two useful jquery plugins to get you started
https://github.com/BenGriffiths/jquery-save-as-you-type which saves what you type locally to a cookie client-side. Using this plugin is simpler and you will only need to actually save into your django backend when the user hits your form's "save" button.
and
https://github.com/nervetattoo/jquery-autosave which requires that you set up an intermediary model (perhaps a Draft model) in django to keep track of updates along the way. And when the user finally hits save, a final copy of the data is then saved into the actual model.

form from multiple models

I need to create a form where the rows are a list of users and the columns are all the user's permissions. The models are the default django User's and django Permission's models.
How can I do this things? Are there a way to do by django forms or I must do manually with django and js?
If you just want to view this data, extract the relevant information (User and Permission instances), and then generate a table using django templates. Django intentionally does not 'bless' any css or js frameworks, so you are required to do the html yourself (using tools of your choosing).
Forms aren't going to help you here I don't think. I advise you to draw up what it is you want to see, and start figuring out what the html is going to need to look like. Forms in django are simply a mechanism for passing data between django and the user. You are free to render them how you wish.

Django 1.4 wizard and tables2 navigation don't mix

We're using Django 1.4's new wizard to create, well, a wizard. We have a wizard where, a few steps into it, the user has to select a row from a listview/datagrid/table. We use Django-tables2 to show this data.
The problem is that django's wizard has a single fixed URL, and uses a hidden form field that tells the wizard at what step it is. So all forms submit via POST back to the very same URL, and Django's wizard figures out from which page the user comes, stores the submitted data and based on the hidden form field, figures out where to go next.
Django-tables2 is an HTML grid that supports paging and sorting through a set of data. However, it does so using http GET, passing some querystring variables to indicate what column to sort and/or what "page" of data to show.
As soon as we use sorting or paging in a tables2 grid inside a Django wizard, the GET will call the same URL, because it is a GET, the Django wizard will not receive the hidden form values it expects that regulates navigation, and it will happily show the first page of the wizard by default.
I'm wondering if anyone has experience with this and knows of a solution to keep both the Django Wizard as well as the Tables2 functional.
Thanks in advance,
Erik