2👍
Depending on the results you are looking for, there are multiple ways to solve this:
1. Use CreateView
and UpdateView
Django already provides some classes that render a form for your model, submit it using POST
, and re-render the form with errors if form validation was not successful.
Check the generic editing views documentation.
2. Override get_context_data
In LandingView
, override TemplateView
‘s get_context_data
method, so that your context includes the form you are creating in create2
.
3. Use FormView
If you still want to use your own defined form instead of the model form that CreateView
and UpdateView
generate for you, you can use FormView
, which is pretty much the same as TemplateView
except it also handles your form submission/errors automatically.
In any case, you can keep your search
function inside the class-based view and call it from get_context_data
to include its results in the template’s context.