How to pass number input model data between two templates/views?

I’m trying to create a primitive online store functionality, and I want the end user to be able to input the number of items they want to order in one “front page” view and display that data in a second “basket” view. Should I just rebuild my code using forms, or is it possible to do this in default Django code? Here is the code for my current (unsuccessful) attempt: am I on the right track?

Django models page that gives context for the templates:

class Product(models.Model):
product_text = models.CharField(max_length=200)
price = models.IntegerField(default=5)
orderedamount = models.IntegerField(default=0)
stock = models.IntegerField(default=10)
def str(self):
return self.product_text

Views page:

def orderscreen(request):
product_list = Product.objects.all()
context = {
‘product_list’: product_list,
}
return render(request, ‘orderscreen.html’, context)

def basket(request):
product_list = Product.objects.all()
for p in product_list:
context = {
‘orderedamount’: p.orderedamount,
}
return render(request, ‘basket.html’, context)

First “front page” view template:

<!doctype html>

Products: {% if product_list %}
    {% for p in product_list %}
  • {{p.id}}. {{ p.product_text }}: {{ p.price }}€
  • {% endfor %}
{% endif %}

Second “basket” view:

<!doctype html>

Current order: {% if product_list %}
    {% for p in product_list %}
  • {{ p.product_text }}:
  • {% endfor %}
{% endif %}

First note: When you post code snippets here, please insert them between lines containing only 3 backticks - ```. (Make sure you use the backtick - ` and not the apostrophe - '.) That means you would have one line with the 3 backticks, your code, and then one more line with just the 3 backticks. That allows this forum software to format your code nicely:

# The line above this is just ```
def function(parm):
    return parm
# The line after this is just ```

If you want, you can edit your post to put the backticks before and after each of the code blocks you’ve posted - that’ll make it all easier for everyone to read.

Using forms is “default Django code”. That’s part of what makes Django such a usable system. You can get started without using them, but if you work on anything other than a “toy” project, you’ll find that you’re doing a lot of work that forms would do for you.

If you haven’t already done so, I always recommend people start with working their way through either (or both!) of the Official Django Tutorial and the Django Girls Tutorial . There’s a lot of useful information contained in them demonstrating how the different Django components all work together as an effective system.

Beyond that, you might also want to review Working with forms.

1 Like

I’d read the official tutorial already, so I just assumed that whatever was covered in that was the default way of doing things. Thanks for putting me on the right track. I can’t seem to be able to edit the OP anymore, though.

The tutorial is just an introduction to help get you started. There are many features within Django not covered within it, which is one reason why I also recommend the Django Girls Tutorial.

Beyond that, the What’s next? paragraph at the end of page 7 provides a link to the What to read next page.