B
B
Bjornie2017-03-21 19:48:53
Django
Bjornie, 2017-03-21 19:48:53

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

2 answer(s)
G
GDApsy, 2017-03-21
@GDApsy

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/')

But you can not implement the post method, but only implement the get method, and in it process the HttpRequest.GET property of the request object: https://docs.djangoproject.com/en/1.10/ref/request...

D
Denis Sh, 2017-03-22
@Deq56

about URLs
more details https://djbook.ru/rel1.7/topics/http/urls.html#nam...

def search_results(request, param1, param2):
    pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question