3

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?

2 Answers 2

6

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.
Sign up to request clarification or add additional context in comments.

2 Comments

It didn't work , def roll_number(request): if request.method == 'POST': roll_number = request.POST['roll_number'] print(roll_number)
I'm sorry I was editing the comment , got deleted by mistake
3

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.

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.