[Answered ]-Displaying and processing a django form

1👍

I believe when you submit the form you are getting a 404 page displaying no url matches found. This is because you are trying to use the reverse resolution of urls in Django but you haven’t used jinja tags in the url name.

{% extends 'products.html' %}
{% block title %}Оформление заказа{% endblock %}
{% block content %}
    <h1>Оформление заказа</h1>
    <form method="post" action = "{% url 'product_buy' <uuid_param_here> %}">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="Submit" name="submit" value="Купить"/>
    </form>
{% endblock %}

uuid_param_here => is the param that you need to send in the request url.

{% url ‘product_buy’ someproduct %} is equivalent to productsHTML/someproduct/buy

Here’s [a link] https://stackoverflow.com/a/42441122/10669512! to a similar problem. Please do check.

Also, your views seems incorrect. Since you are using try/except, the exceptions are not properly handled. You have not defined form variable inside post condition

if request.method == 'POST': # If the form has been submitted...
    form = OrderForm(request.POST) # A form bound to the POST data
    if form.is_valid():
        product = Product.objects.get(id=uuid)
        email = form.cleaned_data['email']
        phone_number = form.cleaned_data['phone_number']
        order = Order.objects.create(email=email, product=product,phone_number=phone_number)
        return redirect('productsHTML')

Check this for more clarification [a link] Django form is_valid() fails

Leave a comment