Maddie Graham
Maddie Graham

Reputation: 2177

Transfer of data from the button to the post form on the next page. Django

I am trying to pass the get parameter from the previous page assigned to my button to the next page add it to my form and save it in my database. My code looks like this:

views.py

def massage_order(request):
    parametr_html_product_name = request.GET('parameter')
    if request.method == 'POST' and 'btn_massage_order' in request.POST:
        ordering_form = OrderingMassageForm(data=request.POST)
        if ordering_form.is_valid():
        massage_product = parametr_html_product_name
        [...]
    else:
        ordering_form = OrderingMassageForm()

templates.html

<a href="{% url 'app:massage_order' %}?parameter={{ product.name }}&parameter_1={{ product.price }}&parameter_2={{ product.time }}&parameter_3={{ masseur.name }}">
    <div class="btn-group btn-action-label" role="group" aria-label="Like">
        <div class="btn btn-sm btn-outline-primary btn-label">{{ product.price }} zł/ {{ product.time }} min</div>
        <button type="button" class="btn btn-sm btn-primary btn-action">
            <span>Zamów masaż</span>
        </button>
    </div>
</a>

But I receive a mistake every time 'Multi Value Dict Key Error' i found his solution parametr_html_product_name = request.GET.get('parameter', False) but in this situation, my variable is False and not the name of the product (what it expects). How can I pass the variable 'html parameter product name' to my form? I tried to add it to the list (also without a positive effect). Any help will be appreciated.

Why, despite adding a variable to the Django list, does not it see in my form?

list = [parametr_html_masseur_name, parametr_html_product_name]

[...]
massage_product = list[1]
[...]

EDIT

templates.html

<form action="." method="post">
    <input type="hidden" name="name" value="{{ product.name }}">
    <input type="hidden" name="name" value="{{ product.price }}">
    <input type="hidden" name="name" value="{{ product.time }}">
    {% csrf_token %}
    <div class="btn-group btn-action-label" role="group" aria-label="Like">
        <div class="btn btn-sm btn-outline-primary btn-label">{{ product.price }} zł/ {{ product.time }} min</div>
        <button type="submit" name="hidden_btn" class="btn btn-sm btn-primary btn-action">
        <span>Zamów masaż</span>
        </button>
    </div>
</form>

forms.py

class HiddenOrderingMassageForm(forms.Form):
    product_name = forms.CharField(widget=forms.HiddenInput)
    price = forms.IntegerField(widget=forms.HiddenInput)
    time = forms.IntegerField(widget=forms.HiddenInput)

views.py

if request.method == 'POST' and 'hidden_btn' in request.POST:
    hidden_form = HiddenOrderingMassageForm(request.POST)
    if hidden_form.is_valid():
        product_name = hidden_form.cleaned_data['product_name']
        return HttpResponseRedirect(reverse('app:massage_order', args=[product_name]))
else:
    hidden_form = HiddenOrderingMassageForm()

urls.py

url(r'^massage_order/(?P<text>[\w\-]+)/$', views.massage_order, name='massage_order')

Upvotes: 0

Views: 1255

Answers (1)

mfrackowiak
mfrackowiak

Reputation: 1304

In your current code, you are using GET (as everything is wrapped in <a> tag), yet you expect the values to be in request.POST. I think that the best way to do it would be to use a form with hidden inputs with all values as you require, and validate them on backend, as you do with form. So, instead of using a, change to something following:

<form action="{% url 'app:massage_order' %}" method="post">
    <input type="hidden" name="name" value="product_name">
    ...
    {% csrf_token %}
    <div class="btn-group btn-action-label" role="group" aria-label="Like">
        <div class="btn btn-sm btn-outline-primary btn-label">{{ product.price }} zł/ {{ product.time }} min</div>
        <button type="button" class="btn btn-sm btn-primary btn-action">
            <span>Zamów masaż</span>
        </button>
    </div>
</form>

Or, instead of creating the form manually in the template, you could specify an OrderingMassageForm which would have all fields' widgets set to hidden input, i.e.:

class HiddenOrderingMassageForm(forms.Form):
    product_name = forms.CharField(widget=forms.HiddenInput)

and then put in the template in usual way:

<form action="{% url 'app:massage_order' %}" method="post">
    {{ hidden_form }}
    {% csrf_token %}
    <div class="btn-group btn-action-label" role="group" aria-label="Like">
        <div class="btn btn-sm btn-outline-primary btn-label">{{ product.price }} zł/ {{ product.time }} min</div>
        <button type="button" class="btn btn-sm btn-primary btn-action">
            <span>Zamów masaż</span>
        </button>
    </div>
</form>

Upvotes: 2

Related Questions