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?
Related
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.
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.
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.
I would like to have both a contact form, a newsletter form and a photo slider/portofolio in index.html. Everything drawn into this page only.
Am I correct to assume it has something to do With "URL dispatcher" in the documentation? And could someone please help me with some examples on how to point everything to the same URL?
Want everything to redirect back to the index when done, after email has been sent, after registering for newsletter and so on. Just to explain better what I actually mean here as I don't have the knowledge to do it in correct terminology.
Thanks in advance for all the help I can get.
As a possible solution you can create index page with multiple forms. Each form will redirect on post to it own view, for example:
First form:
<form action="{% url 'myapp:index_one' %}" enctype="multipart/form-data" method="post">
Second form:
<form action="{% url 'myapp:index_two' %}" enctype="multipart/form-data" method="post">
In each view you create all the forms and pass them to index.html
def index_one():
indexForm1 = index_form1()
indexForm2 = index_form2()
if request.POST:
indexForm = index_form1(request.POST, request.FILES)
//process first form here
//load index.html and pass context with forms
In second view you do the same but on POST you process another form:
def index_two():
indexForm1 = index_form1()
indexForm2 = index_form2()
if request.POST:
indexForm2 = index_form2(request.POST, request.FILES)
//process second form here
//load index.html and pass context with forms
I have such solution in production and it is working fine.
Let's say I have many posts and each post has a url example.com/post_id. Also there is a HTML form after this post where one can submit comment. If I had to submit this comment to this url: example.com/submit , then I could just do <form action="submit/" method="post">. But, I want this comment to be submitted to this url: example.com/post_id/submit so that when it's view is called, that view has access to post_id. This is so I can store the entered comment along with post_id in the database. (I can access the request url in view by request.path_info.)
One approach would be to pass {{request.path}} concatenated with "submit" in the HTML form action in the template. But I am not able to do that. One can do {{value|add:"submit"}}. But how do I put {{request.path}} in place of value?
tl;dr Using django templates, how to I pass post_id/submit url to HTML form action. (Here current url is example.com/post_id.)
It's a bad idea to try and parse/modify the existing URL. But there's no reason to. Your template presumably already has access to the post itself, so you should use this to construct the URL via the normal {% url %} tag.
<form action="{% url "submit_comment" post_id=post.id %}" method="POST">
assuming the post is passed to the template as post and there is a urlconf that looks like this:
url(r'(?P<post_id>\d+)/submit/$', views.submit_comment, name='submit_comment'),