Django form post request without rendering - django

I have django form, then for each field I set initial value. How can I make POST request in my view.py (like after pressing submit button) without rendering the form. I just need to send these initial values as POST request to another url.

You can create the form in HTML and set display:none is style attributee of each field. It will hide the form from front-end and when you click on submit button it will send all data in POST request.
<form method='post'>
<input name='field_name' style='display:none' value='abc'>
<input type='submit' value='submit'>
</form>
Above code will show only submit button on front-end and by clicking on it, it will send value of input field in POST request.

Related

A create view in another view django

I am new to django. I've created a basic posts app where users post messages. The project has posts app for posting messages. The app has two models- Post and Comment where one post can have many comments. My posts are shown in list view with comments for a particular post.
Now, i want to have an 'add comment' button for each post so i can directly add comment (stack exchange design!) . This can otherwise be implemented by a seperate createview for comments model. I want an textarea on listview itself to input comments for a post.
How can i do this?
In the most basic form, you can define different routes for your desired views (CreateComment, PostDetails) and send a POST request to the CreateComment view from the PostDetails route.
urls.py
urlpatterns = [
path('post/<pk>/', post_details, name='post-details'),
path('comment/new/', add_comment, name='create-comment'),
]
and in your post/<pk>/ route, set up a form like below:
<form method="POST" action="/comment/new/">
<input value="" name="description" type="text" />
<input value="/current/path/" name="redirect" type="hidden" />
{% csrf_token %}
</form>
You can then redirect the user to the page he was making the request from in the first place, using the redirect value which is passed by the form from the origin route.

html - redirect to new url after form submission (django site)

I am trying to make a button redirect the user to a new url after they submit a form. This is how it is right now, and it works properly and all the data gets sent to the django database.
<form method='POST' action='' class="col-md-6 col-md-offset-7" style="background-color: lightgreen; border-radius: 10px">
However, when I change the action to
action="{% url 'ridesharing:request-success' %}",
the redirect works, but the data does not go to my django database.
What is going on here?
You seem to have some confusion here. The action of the form is where the browser will send the data. Obviously, if you don't point that at the view which actually processes the data, then it won't be saved.
To redirect after a post, your view should return a redirect.
When you declare a form with an empty action attribute:
<form method='POST' action=''>
the POST data will be sent to the same URL. This is useful because if there is some error in the form, it's easy to re-display it with all fields filled with values entered by the user. Then, when the form becomes valid, a redirect is done to the confirmation page.
When you declare your form that way:
<form method='POST' action='{% url 'ridesharing:request-success' %}'>
The data entered by the user in the form will be sent to the view request-success. But this view probably only render the template of the confirmation page. If you want to correctly handle data from the form, you have to set action attribute of your <form> to the same view, or easier, keep it empty.
I do not understand why Daniel Roseman's post isnt accepted as the answer. It helped me when I wanted to redirect a create form to its update form.
Basically in the view of the app I defined the get_success_url to reverse_lazy to the data-update.
def get_success_url(self):
return reverse_lazy('app_name:data-update')
Just replace the 'app_name:data-update' with the appropriate url.

Mailchimp form double action

Does anybody know how i handle multiple actions on a form. In my case i want to add an action that sends a subscribed member of a newsletter to Mailchimp but i all ready have form action on the html form. Does anybody know a work around?
<form action="/form/post/13" id="cta-form" method="post" enctype="multipart/form-data" data-h5-instanceid="0" novalidate="novalidate">
as you can see i all ready have an actions which pushes the data into the database.
But i also need this:
<form action="http://mailchimp.us8.list-manage.com/subscribe/post" method="POST">
how can i do this?
Are the form tags nested? that could be the problem.
have a look at this post: multiple form tags in page or one form tag?

How Will the Inclusion of Two Forms Affect my CSRF Token Use?

I am attempting to create a page that includes two forms: one that is visible when the page loads (a signin form), and a second that appears in a modal if the user clicks a button (a signup form).
I am using Django, and, although I am still figuring out how I will handle these forms, my largest concern is how the CSRF token will play into all of this. For example, should I use {% csrf_token %} inside of only one of my <form></form> tags, or should I place it in both?
Further, if I do use it in both forms, will this affect my POSTS to the server in any way? Currently, I am taking the data in a form (depending on which submit button is clicked) and POSTing this way:
var data={
'username':$('#username').val(),
'password':$('#password').val(),
'csrfmiddlewaretoken': '{{ csrf_token }}'
}
$.post("/", signin_data);
csrf_token should be placed in both the forms, as long as both are being accessed on the server side via GET or POST, and YES you can use the same csrf_token for both the forms without any issues.
You can do something like
<form action="." >{% csrf_token %}
{{form1.as_p}}
</form>
when you do data=form.serialize(), the csrf token is automatically serialized in the data of the ajax request.
The reason multiple {% csrf_token %} works is because all the token does is provide information for validation that a form request is from a valid (untampered) user session.

Django request.POST does not contain the name of the button that submitted the form

I have a django form with two different submit buttons, on the view where the form is submitted to I need to know what submit button was pressed and take different actions accordingly.
From what I have read the submit button's name or id should be somewhere in the request.POST dictionary, but it not there!
This is a fragment of my form:
<form id="editPaperForm" action="{{paper.editURL}}" method="POST">
<input type="submit" name="savePaperButton" id="savePaperButton" value="Save and Send Later"/>
<input type="submit" name="sendPaperButton" id="sendPaperButton" value="Save and send"/>
...
</form>
In the view:
...
if 'sendPaperButton' in request.POST:
return applicants_confirmSend(request, paperID)
else:
return applicants_home(request)
sendPaperButton is never in the request.POST, and neither is the other one, should I be looking somewhere else?
The only idea I have is to add a hidden field and modify it via javascript before sending the form but that seems kind of redundant since I'm pretty sure that data should be there somewhere...
Thanks!
Don't forget to add the name and value parameters to your "button" or "input type=submit" fields of the form. I've had the same problem once and it drove me crazy.
In short, as request.POST contains a dict, you need a key and a value. The key corresponds to the name parameter of your button, and the dict's value to the button's value.
<button type="submit" value="preview">Preview</button>
won't be reflected in request.POST (there's no key for the POST dictionary!), whereas
<button type="submit" value="preview" name="preview">Preview</button>
will have a key "preview" with value "preview".
For some reason, in Chrome, when I had two buttons using <input/> tags, it would actually treat the button I didn't click as an input. That way, when I tested something like 'sendPaperButton' in request.POST, it would return the opposite of what I wanted.
I changed these to <button></button> tags and it worked fine.