D
D
Dao1312020-02-08 21:43:11
Django
Dao131, 2020-02-08 21:43:11

How to properly redirect to a django admin form with a POST pass?

I have a page template item.html, in this template there is a form:

<!--shop/templates/shop/item.html-->
 <form action="{% url 'admin:supplier_prodsupp_add' %}" method="post">
    {% csrf_token %}
    <input type="hidden" name="product" value="{{ item.id }}">
    <input type="submit" class="btn btn-primary mb-3" value="Создать новую связку">
</form>

I want that when you click on the "Create a new link" button, there will be a redirect to the django admin form page and that the field will be pre-filled there, the data for which is taken from the hidden form input on the template page.
The model itself:
#supplier/models.py
class ProdSupp(models.Model):
    product = models.ForeignKey('shop.Product', on_delete=models.CASCADE, verbose_name='Товар')
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, verbose_name='Поставщик')
    art = models.CharField('Артикул', max_length=15)
    url_prod = models.URLField('Ссылка')
    price = models.DecimalField('Цена', max_digits=9, decimal_places=2)

    class Meta:
        verbose_name = 'Связка товар поставщик'
        verbose_name_plural = 'Связки товар поставщик'
        unique_together = ('product', 'supplier')

    def __str__(self):
        return str(self.product)


And this is the display of the page template:
5e3effa559feb258783920.png

If done in the forehead and in the form in the action attribute, write the path: Then a redirect will occur, which I will need, the field will be filled based on the data from the hidden form, but there will be an error on the remaining fields and it seems to me not true. Screen below. Therefore, I want to know how to properly redirect with passing data to the django admin form so that errors do not occur. Screen:

{% url 'admin:supplier_prodsupp_add' %}


5e3f00b8ae2fa197425552.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question