5

I have created a Class view in views.py of the django application.

class HelloTemplate(TemplateView):
    template_name = "index.html"

    def get_context_data(self, **kwargs):
        context = super(HelloTemplate, self).get_context_data(**kwargs)
        return context

Now I have a form defined in the html page:

<form method="get">
    <input type="text" name="q">
    <input type="text" name="q1">
    <input type="submit" value="Search">
</form> 

As you can see, I am submitting the form on the same page.

Now I want to get the form submitted values in my HelloTemplate class. I don't want to create another class or methods outside the existing class.

Also, I would like to send an error message to the html form if data is not validated in the django.

I don't know how to do this, please help me out.

3 Answers 3

5

You need to define get (because your form defined with get method <form method="get">) method in view class:

class HelloTemplate(TemplateView):
    template_name = "index.html"

    def get_context_data(self, **kwargs):
        context = super(HelloTemplate, self).get_context_data(**kwargs)
        return context

    def get(self, request, *args, **kwargs):
        q = request.GET.get('q')
        error = ''
        if not q:
            error = "error message"
        return render(request, self.template_name, {'error': error})

More information in django docs here Introduction to Class-based views

Sign up to request clarification or add additional context in comments.

5 Comments

How to send the errors to the same page, if input is not valid? And can't we use "dispatch" method?
@NikunjAggarwal you can send errors or any other data to page in template context. dispatch method looks at the request to determine whether it is a GET, POST, etc, and relays the request to a matching method if one is defined, you can redefine it if you need to.
i tried using dispatch method but it is not working for me, can You give me an small example in context to my code? It will be helpful.
I dont understand why you want to redefine dispatch in your class. If you use get - define get method, if you use post - define post and dispatch will call one you need according to request. Anyway if you want: def dispatch(self, request, *args, **kwargs): #some code. That it, just like post or get
check View class source in django if you want to understand: views/generic/base.py
0

There's only one value, and it's in request.GET['q'].

Comments

0

Quick response, I can show you what I did a while ago for a review form (for people to create a new review, one of my models):

def review_form_view(request):
    c = {}
    c.update(csrf(request))
    a = Review()
    if request.method == 'POST':
        review_form = Review_Form(request.POST, instance=a)
    if review_form.is_valid():
        a = review_form.save()
        return HttpResponseRedirect('../dest_form_complete')
        pass
    else:
        review_form = Review_Form(instance=a)
    return render_to_response('../review_form.html', {
        'review_form': review_form,
    }, context_instance=RequestContext(request))

If you have a user model, comment model, etc. you can probably use something similar to this. Very (very) roughly put, the request is the input that the user fills out in the form, 'POST' is the method called that lets the server know you are adding entries to your database, and is_valid() validates the data according to your models.py parameters (can name be NULL? Is age an integer? etc).

Take a look at https://docs.djangoproject.com/en/dev/topics/forms/ as well for more examples and explanation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.