I
I
Ilya2016-02-16 15:22:23
Django
Ilya, 2016-02-16 15:22:23

How to pass parameters to django view?

There are models.
models.py

class Product(models.Model):
    title = models.TextField(verbose_name='Номер',blank=True)
    producte = models.TextField(verbose_name='Продукт')
    order = models.ForeignKey('orders.Order', verbose_name='Заказ')

class Order(models.Model):
    title = models.CharField(verbose_name='Название заказа', max_length=60)
    slug = models.SlugField(verbose_name='URL',unique=True)

Representation
# view.py
def ViewOrder(request, slug):
    order = Order.objects.select_related().get(slug=slug)
    starts = order.start_set.all()
    id = Order.pk

urls.py
url(r'^(?P<slug>[-\w]+)/$', ViewOrder, name='view_order'),

forms.py
class ProducteForm(ModelForm):

    class Meta:
        model = Producte
        fields = ('title','producte', 'order')

I go to the order page, and all products from this category are displayed there. I don’t understand a little how to pass parameters, categories. That is, I want to make a button on the page with a category to create a product, and when I click to create a product, the order field in the Producte model is filled with the current category in which I am.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-02-16
@sim3x

porridge, it turns out somehow
This is how it should be from your words

class Category(models.Model):
    name =....

class Product(models.Model):
    category = models.ForeignKey(Category)
    title = models.TextField(verbose_name='Номер',blank=True)
    producte = models.TextField(verbose_name='Продукт')


class Order(models.Model):
    products = models.ManyToMany(Product)

In order for a predefined value to appear in the form, you need to smoke https://docs.djangoproject.com/en/1.9/ref/forms/ap...
- this will set the value of the current category
https://docs.djangoproject.com/en/ 1.9/ref/forms/ap... - this is how you indicate that the field was not shown to the user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question