I'm developing an application using Django. The main page contains a textarea input where you must enter a list of IDs(this list can be large, so I submit the form using POST). After submiting the form, the server will render a page with a table where you can find informations about every ID. If you click on an ID, you will go to a page where you can modify some fields. For saving the modified fields into the database, the form action is: 'manage_ids/<>/save' and on the server I have a view for this url. How can I redirect the user back to the page with the table? I'm submiting the form using POST so a simple redirect wouldn't work. Or, to remain on the page where you can modify the fields(in this case, if you press the 'Back' browser button, to be redirected to the table page).
Workflow:
/
POST
/manage_ids
GET
/manage_ids/<<md5>>
POST(save)
/manage_ids/<<md5>>/save
Now, I should be redirected to:
/manage_ids
If you need to preserve a long list of IDs across multiple page requests, you should use the session.
I'd like to recommend you take a look at Django Form wizards
Related
I have created a Getting Started page to which I have to send the users if they are logging in for the first time after registration.
I also referred to this question but didn't understand anything
If not I have to redirect them to the Home page.
I have set the LOGIN_REDIRECT_URL to 'home' in settings.py
I have not tried anything yet because I have no idea about how I should do it. Please assume a simple html for both the pages with an <h1> tag containing their respective names
There are multiple ways to achieve this, and this are three that I usually do:
you can put additional field in user model, for example seen_getting_started that is boolean and during processing of home template in views.py you can check for this field and redirect accordingly to getting started page. I have an option that user needs to select his language and date formats, so if it not changed and stored in his profile than "select language and date format" page pops out
you could also check his creation time, if it is created in last 10 minutes it is safe to assume that the user is new, but there might be some issues with that if you use for example mail check during registration
if you are using mail check during registration, than you need to lend user on "email confirmed" page that could also lead to your getting started page.
Hope it helps. Sure that other options might work too.
i'm trying to make a user registration with multiple fields to save in the same model. The idea is have a form in the home page with username and password and when press button continue the user is redirected to another page with multiple fields to insert personal info for the user profile.
I imagine i must have two different views for that, but i don't understand how put all this data together.
Thanks!
You could use the FormWizard (https://django-formtools.readthedocs.io/en/latest/wizard.html)
Basically you would have one form for user/password and another form for the other fields.
Take a look
In your case i would prefer manipulate HTML content with JavaScript and after everything is ready, just send an ajax call.
So, what issue im running into is how do i know what element of my page made a post request? I have multiple elements that can make the post request on the page, but how do i get the values from the element that created the request? It seems like this would be fairly trivial,but i have come up with nothing, and when doing quite a few google searches i have come up with nothing again.
Is there any way to do this using Bottle?
I had an idea to an a route for an sql page (with authentication of course) for providing the action for the form and use the template to render the id in the action, but i was thinking there had to be a better way to do this without routing another page.
You could add a hidden input field to each form on the page with a specific value. On the server side, check the value of this field to detect which form the post request came from.
I am new on web development.
I am now implementing a simple create/edit user form with a submit button.
I would like to know the better practice to implement this.
I have already define this kind of web api
URL Method Description
/users/ GET Gives a list of all users
/users/ POST Creates a new user
/users/<id> GET Shows a single user
/users/<id> PUT Updates a single user
/users/<id> DELETE Deletes a single user
My first approach:
i create two new "/user/add" and "/usr/edit" function,
which similar to
app.route("/users/edit")
def edit_user(){
....
....
call the internal api /user/ with a "put" method
....
render_template("edit.html")
when I click the submit the button, the i call the above internal api /users/ ,method=PUT,
and render the final template.
My second approach:
in my internal api /user/, i try to read the http header to see if i want a html template or json text and return back to user
Say, again when i want to create a edit form, instead of calling /user/edit , i call /user/, with a PUT method
def put(self, id):
//see the header of that request
if header == html
render_template("edit.html", .....)
if header == json
update the record
#
my question , basically, i don't know if "/user/add" "/user/edit" route is necessary to make a form, or we can just simple embedded into /user/ api with different "post" or "put" method.
the idea is coming from here , from flask, pluggable view, which i am wondering how to make a better implementation
Or is that a better way to do it???
Thanks a lot.
I would choice your first approach because than your URLs are clear and logical. Also you split your frontend (Website with forms) and backend (API) which is in testing very helpful. A normal web browser makes only GET and POST requests to a website so it is very difficult to render template by a PUT request for the user because the user is normally not able to start a put request.
In my Django app I have multiple pages displaying a link that loads a new page displaying a form. When the form is submitted, what is the cleanest way to redirect to the originating page from which this form was accessed?
originating page -> form page -> originating page
Using a next variable seems unellegant since I have to set it as a GET variable on the originating page link, and then set it as a hidden POST variable in my form? Any other ideas would be appreciated.
There are a couple of options, all with the cons and benefits ofcourse.
passing the originating page withi POST/GET
storing the originating page in the session (won't work with multiple tabs obviously)
storing the originating page in a cookie (won't work with multiple tabs either)
if it's a single page, redirect to the referrer. Doesn't seem possible in your case
Personally I think using a next parameter is your best option, but do remember to secure it (only relative urls, no javascript stuff, csrf framework) so you won't have any security problems with it.
WoLpH already listed the resonable possibilities and pointed to (probably) the best of them as a solution. So I will only elaborate on it.
If you need to handle this: originating page -> form page -> originating page then in reality it will look like this: originating page --[GET]--> form page --[POST/submision]--> form page(2) --[GET/redirect]--> originating page. This means, that form page(2) stage has to somehow know where to redirect user and the only reasonable choices you have here is to use session (with the drawback mentioned by WoLpH) or to pass it in POST, possibly as a hidden field.
Regarding the first GET and passing next URL into form page, you can either pass it in the query string (which you find unelegant), or you can extract it from HTTP_REFERER header - which won't work for more paranoid users like me.
You can also do a mixed stuff here, i.e. add next=<next-URL> into the query string if and only if user hasn't turned off HTTP_REFERER in her browser. Then most users won't see any ugly stuff in URLs.
Note that all these things can be automated, i.e. you can write your links as:
<a href="{% url myform %}?{% inject_next_url %}">
When inject_next_url injects sth like: next={{ context['request'].get_full_path() }} only if HTTP_REFERER is not present.
Then, in form handler you can use some generic extract_next_url(request) function, that looks for next URL in one of: query string, HTTP_REFERER, request.POST, or more consise - in one of: HTTP_REFERER, request.REQUEST
Another option might be to create separate URL conf that resolve to the same view, and passing in the source view as a kwargs to the view.