It is a good way to create a view only for submit a form ?
For example :
Ajax send a form submitted to this specific view, this view validate the form and create a object in database and return a json response to Ajax.
It's ok to do like this ?
Because I'am afraid of users write directly the url to this specific view on browser...
Related
I have a Django form that takes filter options for a report. The report page is a separate view that renders the report based on the form data.
My first pass at this, I simply set the action of the form to the report page and method to GET. The form data was then passed directly the report view via the querystring which I would use GET to retrieve. The problem with this was that this bypassed form validation since the the form did not post back to its own view.
My next pass, I removed the form action (so it would post back to itself) and used form_valid (I am using class based views) to encode the form data and redirect to the report view like so:
ReportOptionsView(FormView)
form_class = OptionsForm
template_name = 'my_report_options.html'
report = reverse_lazy('my_report')
def form_valid(self, form):
qstr = urlencode(form.cleaned_data)
return redirect(self.report+"?"+qstr)
The report page works the same -I just retrieve the information from the querystring to filter the models and display the report.
I would prefer the form data not appear on the querystring of the report page. When I tried to redirect to the report page using a POST method is where I starting having trouble. Even going back to my original flow setting the form action to the report page (thus losing validation) and setting the form method to POST, I got 405 errors. I realize that there may be ways to do this using Javascript, but would prefer to stick with Django/Python
My question is, what is the proper method in Django to take cleaned data from a validated form and POST that data to separate view so that the form data is not exposed in the URL?
I am struggling with the update of database information with the forms, and simply passing information between views. I could really use some advice because I am fairly new to Django.
The flow goes like this:
1. First form; I transfer the article price and title to the view "event"
2. The view "event" handles title and price and ask for confirmation in the html form
3. Once confirmed, it directs that information to the view "transact_test", I want this view to handle the update of the database via a new form that is build with the Article model. But it provides the error message : "didn't return an HttpResponse object. It returned None instead."
To fix your error: In transact_test you are just calling render in the request.method == 'POST' block:
render(request, ...)
You need to return render:
return render(request, ...)
You should really take a look at some additional django tutorials you are making this harder than you need to. You should almost never manually render a form when using django. And as Tariq said, please don't use images.
I added a Django form to my Bootstrap nav bar to be included on every page, and it renders as it should with the appropriate values. The form was added using an inclusion_tag. However, I'm now at a loss as to how to handle the request from the form. Upon submission, whichever page the user was on should reload with updated content from the form submission. For more context, see my earlier question: How to place a django form in a nav bar so that it appears on every page?
Answering my own question (again). To handle a request from a form that appears on every page and loaded via a custom template tag, create a url path and corresponding view -- e.g. '/form-submission/' and form_submission_view. In the view, handle the form processing logic as you normally would for a POST request, but then return a redirection back to whatever page the user was on when the form was submitted, like so:
return redirect(request.POST.get('path'))
I have a Django form with two dropdown integer fields and a submit button.
A user selects values from these dropdown fields and click on submit button.
The Django template performs a form 'post' and sends these two dropdown values to my Django views.
Django views performs the form validation by validating these two dropdown fields values.
If the form is not valid, it will send the error message to the Django template and I can display the error message on the same form page above the form.
If the form is valid, then it will redirect to a new URL.
Issue: How do I tell my Django view to redirect the URL to a new tab only after the form is valid?
I'm assuming that you're already familiar with Djangos Form View. Within the form_valid(self, form) method of your class based view simply replace return super().form_valid(form) with redirect(reverse('YOUR_TARGET_VIEW')). If you have other mixins within your view that rely on form_valid you may also call super().form_valid(form) prior to returning the redirect reponse but without returning its returned data further.
As of displaying error messages you may refer to the django message framework.
EDIT: A cleaner way to do this, may be to overwrite success_url within your processing but this may not be appropriate to all use cases as you need to provide usefull defaults within each request path.
Django beginner.
I need to get user's choices and show the information to the user based on the data entered by the user.
For this I am using a form to fetch the data from url1 and I am posting the data to another url, url2 whose view function will process the data and show the information. All working fine.
I don't want to save the form's data into the database, that's why I am not redirecting after validating the form in url1 views' post method.
POST data can't go with redirects.
If I validate as well as process the form in url1 then I need to display the information in url1 itself, I need the urlname changed to url2 when displaying the information.
Is there any standard way to do this thing?
You should use the session for that. Save the data to the session on submit in view1, then redirect to view2 and get the data from the session for display.