0

I have a designed form in my template and i want to use it instead of Django Forms. I created my model and view class and urls, but I don't know how to receive and use form's data. I've already studied Rendering fields manually but I want to use my form inputs and labels and only need the data.

Please be aware that class and models work perfectly and I can use data if I use {{ form }} in my template instead of html code below

Here's my class in my forms.py:

class Reserve(forms.Form):
    name = forms.CharField(label='نام و نام خانوادگی', max_length=100)
    phone = forms.CharField(label='شماره تماس', max_length=11)
    email = forms.EmailField(label='ایمیل', max_length=100)
    occasion = forms.CharField(label='مناسبت', max_length=100)
    month = forms.CharField(label='ماه', max_length=100)
    day = forms.CharField(label='روز', max_length=100)
    week = forms.CharField(label='روز هفته')
    time = forms.CharField(label='ساعت', max_length=100)
    message = forms.CharField(label='توضیحات')

Here's my function in views.py:

def add_reserve(request):
    if request.method == 'POST':
        form = Reserve(request.POST)
        if form.is_valid():
           name = form.cleaned_data['name']
           phone = form.cleaned_data['phone']
           email = form.cleaned_data['email']
           occasion = form.cleaned_data['occasion']
           month = form.cleaned_data['month']
           day = form.cleaned_data['day']
           week = form.cleaned_data['week']
           time = form.cleaned_data['time']
           message = form.cleaned_data['message']
           reserve = MangReserve(name=name, phone=phone, email=email, occasion=occasion, month=month, day=day,
                              week=week, time=time, message=message)
           reserve.save()
           return HttpResponseRedirect('/')

    else:
        form = Reserve()

return render(request, 'mang/temp.html', {'form': form})

And finally a part of my form:

<form method="post" action="{% url 'mang:add-reserve' %}">
                    {% csrf_token %}
                    <div class="form-group ">
                        <label for="name" class="sr-only">Name</label>
                        <input id="name" class="form-control" placeholder="نام و نام خانوادگی" type="text">
                    </div>
                    <div class="form-group ">
                        <label for="phone" class="sr-only">Name</label>
                        <input id="phone" class="form-control" placeholder="موبایل" type="text">
                    </div>
                    <div class="form-group ">
                        <label for="email" class="sr-only">Email</label>
                        <input id="email" class="form-control" placeholder="ایمیل" type="email">
                    </div>
                    <div class="form-group ">
                        <label for="occasion" class="sr-only">occasion</label>
                        <input id="occasion" class="form-control" placeholder="مناسبت" type="text">
                    </div>.......

2 Answers 2

1

I don't know where you got the template from, but all the fields are missing the vital name attribute. Without that, the browser simply won't send any data to the server.

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

Comments

0

Please add "name" attribute to all the HTML input elements matching your form fields, which I think will solve your problem. An example:

 <input id="email" class="form-control" name="email" placeholder="ایمیل" type="email">

The above input element is given name="email", which matches your form email field.

Once you do this for all the input elements, you should receive the data in request.POST and the form will work as expected.

2 Comments

Thank you so much... what a shame i didn't pay attention to check if form attributes are correct or not
Glad it helped.

Your Answer

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