Django: Form in base template - django

Hi Stackoverflow people,
In my Django project I created a form to register users. This forms can be called through a specific url -> view method. Once the user has filled in the form, presses submit, the same view method will be called and if form.is_valid() is true, then ... simply a standard form, nothing special.
Now, I would like to integrate this little form on every page, and therefore I would like to add it to the base template. I have read that I could populate the form variable through a context_processor, but could I define the process after the submission of the form?
I have created the context_processor.py (as below), added the context_processor to the TEMPLATE_CONTEXT_PROCESSOR dir in the settings (as described here):
from app.forms import Form
def registration_form(request):
return {
registration_form : Form()
}
First of all, the form variable won't be displayed.
And secondly, how do I manipulate the form submission?
I think I misunderstanding the context_processor of Django and would be more than happy about comments on the overall process.
Thank you!

how are you trying to access to form in your template? you probably don't want to use a function name as your dictionary key, maybe you want
return {
'registration_form': Form(),
}
not sure what you mean by manipulate the form submission, but i guess you'd need all the form processing logic in your context processor
if request.POST:
form = Form(request.POST)
# validate etc

instead of creating context processor, create template tag for the purpose and place the tag in base.html
for form submission and displaying errors use ajax, and front-end validations.

Related

How to stage forms in Django?

I have form data that I gather in the views. What the user inputs will determine what the next form will be like. Should I create another view? If so, how do I pass on variables from one view to another? And should the separate view render a different html page?
Or is there a way to work in the same view that I gathered the data initially?
The view looks like this:
def admission(request):
if request.method == 'POST':
form = DeterminingForm(request.POST)
if form.is_valid():
selected_season = form.cleaned_data['season']
selected_batch = form.cleaned_data['batch']
form = DeterminingForm()
context = {
'form': form,
}
return render(request, 'admission.html', context)
I have used django-formtools Form wizard in the past for similar use cases. Subclassing out the SessionWizardView allows you to render multiple different forms using the same view and even the same template if you wish, however you can have a different template for each form. The variables for each form submission are stored in server-side sessions and the context can be extracted for each step in the process.
If the sequence of forms is different dependent on the answers in the previous form, you should be able to build up some logic within the process using the WizardView.get_form() method for each form step in the process. Hope this helps as a starter

How do I redirect a Django form submit to a new tab after form validation?

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.

Sending form to another view django

I am building a website and I want various views that will ask the user to request a quote from our page. I want to keep the code as DRY as possible so I am writing a view quote which will receive the quote requests from various views and, if there is a validation error redirect back to the page that made the request. I managed to solve this using the super bad practice 'global variables'. I need a better solution, I would like redirecting to respective view with the current form so I can iterate through the form.errors. Here is my code:
def send_quote(request):
form = Quote(request.POST)
if form.is_valid():
# do stuff when valid
return redirect('Support:thanks', name=name or None)
quote_for = request.POST['for_what']
global session_form
session_form = form
return redirect('Main:' + quote_for) # Here I would like to send form instead of storing in global variable`
You can use the HttpResponseRedirect function, and pass as argument the page that made the request.
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
All the META data is store on a dictionary, if you want to learn more check the documentation.
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
If you redirect to the referrer, form.errors will be empty, as redirection is always a GET request.
I can think of two solutions to your problem:
Submit forms asynchronously using JavaScript and so populate the errors
Make all the views containing the form support POST - one way to do this would be to create a base class that inherits from FormView
The second option is a typical way of handling forms in Django - you process both POST and GET inside the same view.
After two days of searching I finally found the answer. Instead of saving form in request.session I just save request.POST and then redirect. Here is the code:
def send_quote(request):
form = Quote(request.POST)
if form.is_valid():
# do stuff when valid
return redirect('Support:thanks', name=name or None)
quote_for = request.POST['for_what']
request.session['invalid_form'] = request.POST
return redirect('Main:endview')
def endview(request):
session_form = request.session.pop('invalid_form', False)
if session_form:
form = Quote(session_form)
# render template again with invalid form ;)
Now I can repeat this with all the views I want and just change the what_for input of each form to match the respective view (Like I intended).

Django web application Taking user input and processing it

I am building a web application using django. I need to take a string input from the user and process it using a method I have written myself. How to achieve this in Django? Here are the things I need to do
Get User Input userinput = (string) On start page
Put this string as an argument in my method MyMethod(userinput) and Run it in the backend
Display what MyMethod() returns on another page.
I suggest that you start from django tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
Basically, what you are going to need is form with one text field, HTML template that will render the form, view that will render HTML template with instance of a form when GET request arrives and call your MyMethod with value from form when POST request arrives and URL rule to call your view function on some URL.
Without additional data or any attempt to solve it and concrete problem you encounter - I can hardly offer more help.
You need to create a model with fields which you want to update by user input, then create a form based on this model. Then import this in a view and render it in a template
simple example:
forms.py:
class InputForm(forms.ModelForm):
class Meta:
model = YourModel
fields = ['fields_from_YourModel']
views.py:
from .forms import InputForm
def user_input(request):
input = CustomUser.objects.get(pk=request.user.pk)
if request.POST:
form = ProfileForm(request.POST, instance=input)
if form.is_valid:
form.save()
else:
form = ProfileForm()
return render(request, 'input.html', {'form':form})
Other steps more easier for beginner, you'll find examples in docs

Django wizardview: how to set up step data without validating the form

What I am trying to do I have a view which has a shortcut form which will ask a user to pre-fill some fields before a user is redirected to wizard forms.
For example:
class PreFillView():
def post(self,request):
# get the data from the form and save into request.session
# Then http redirect to the wizard view
Then from this view, I redirect it to a WizardView. In the wizard view, I catch all the information passed in from the previous view in dispatch function:
class MyWizardView(NamedUrlSessionWizardView):
def dispatch(self,request, *args, **kwargs):
#parse data from request.session
#set step data using these data
# Note these data fields only partially covered the form in the wizardview, there is still a couple of fields needed to be filled in the wizard view.
This almost works fine but the only problem is that it validates the form and pop up field error for the fields which are not pre-populated. I tried, if I only redirect to the wizard view without setting the step data, it is fine. It won't validate the form, so no field errors will be displayed.
I am pretty new to Django and not sure if I am doing the right thing and if yes, how can I avoid form to be validated after I set the step data for the current step? Any help will be appreciated.
Implement the WizardView.get_form_initial(step) method in you wizard view class.
This method gets step number as parameters and it should return dict for initial data for the form for that step.