Ashish Pahwa
Ashish Pahwa

Reputation: 125

How to Pass form value to django view?

Is it possible to pass values from custom forms to a function in django view without using models ?

For example in my html I have :

<form method="POST" class="post-form">
<input type="text" name="roll_number">
<button type="submit" class="save   btn btn-default">Search</button>
</form>

Is it possible to pass roll_number to view without creating a model class?

Upvotes: 3

Views: 9878

Answers (2)

Yash Tewari
Yash Tewari

Reputation: 790

aumo's answer describes how you can access attributes sent via POST, and I should be adding this as a comment to it, but I don't have enough reputation.

I don't see an action attribute in your form tag. It specifies where to send the form data.

<form method="post" class="post-form" action="page-url">

Make sure that page-url is a url that corresponds with your various urls.py files. If it isn't, your view won't be called at all. Add print statements to the the top of your view to see if it is being called. The output will appear in your console.

Upvotes: 3

aumo
aumo

Reputation: 5574

Yes, they will be accessible through the request object's POST attribute which behaves like a Python dict, for example in your case:

def view(request):
    if request.method == 'POST':
        roll_number = request.POST['roll_number']
        # You can now manipulate the form data.

Upvotes: 5

Related Questions