Answer the question
In order to leave comments, you need to log in
How to handle form fields?
I have 3 optional fields based on which the database is searched and the result is returned.
I'm trying to figure out how best to make a view if you need to check many fields. Write if request.GET.get('', '') for each input, or is there another, more "jung" way? And if you filled in all the fields at the same time, or two out of three?
Also a question about organizing urls. Now I did this: url(r'^results/', views.search_results, name='search_results'), where query_string with parameters is substituted. What if I want to make beautiful URLs, while the condition remains, the request parameters can be different - how is this done? For example,
Answer the question
In order to leave comments, you need to log in
Perhaps you should pay attention to the following, in CBV you can write two post and get methods:
class MyFormView(View):
form_class = MyForm
initial = {'key': 'value'}
template_name = 'form_template.html'
def get(self, request, *args, **kwargs):
form = self.form_class(initial=self.initial)
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
# <process form cleaned data>
return HttpResponseRedirect('/success/')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question