django views issue?

a small django projects was done i create a book table models as shown:
from django.db import models

class Books(models.Model):
    name=models.CharField(max_length=20)
    x=[('fantasy','fantasy'),
       ('sci-fi','sci-fi'),
       ('coding','coding')]
    category=models.CharField(choices=x,max_length=30)
    photo=models.ImageField( null=True , blank=True, upload_to='pics/')
    def __str__(self):
        return self.name'

this is the view code:

from django.shortcuts import render

from .models import Books
def buy(request):
    return render(request,'pages/buy.html')


def show(request):
    if request.method=='POST':
        x=request.POST.get('buyb')
        print(x)
        
    return render(request,'pages/shopshow.html', {"o":Books.objects.all()})'

and this is the template code:

{% for i in o %}
<div style="display: inline-block; border-style: solid; border: 2px;" >
    <img src="{{i.photo.url}}" style="height: 100px; width: 100px;">
<p style="font-size: larger;">{{i.name}}</p>

<p style="font-size:large;">{{i.category}}</p>
<input type="submit" value="buy" name="buyb" style="width: 70px; height: 30px; font-weight: 50;">


</div>
{% endfor %} 
</div>
    </form>

i need to know how to send the book name,category,image to views so i can send them to another page or etc…

there are 3 divs containing 3 different books show… i need to know how i get to the views which book was clicked and thanks

You would set the “value” attribute of the submit button to the primary key of the book being selected. You can then retrieve the book information from your Books model in the view.

COULD U PLEASE tell me by writing a code ?

What is the primary key of your Books model?

and i set the value to the primary key? how to show the value" buy’ forgive me but i am a noob

shoul i put a primary key in models? it is a keyword or what?

Have you worked your way through the Official Django Tutorial? This is covered in part 2.

1 Like

You’ve got a couple of options here. My recommendation would be to change it from an <input ... to <button ...

@jokersnl93 We cannot tell which method your template code is using. We cannot tell whether the template represents 'pages/shopshow.html' or 'pages/buy.html'.

’ from django.db import models

class Books(models.Model):
name=models.CharField(max_length=20)
x=[(‘fantasy’,‘fantasy’),
(‘sci-fi’,‘sci-fi’),
(‘coding’,‘coding’)]
category=models.CharField(choices=x,max_length=30)
photo=models.ImageField( null=True , blank=True, upload_to=‘pics/’)
id= models.BigAutoField(primary_key=True)
def str(self):
return self.name

i write the primary key code in modesl. i cant view the primary key in admin page and when i click buy i cant get the book info it return none

this is vew and x return none
'from django.shortcuts import render

from .models import Books
def buy(request):
return render(request,‘pages/buy.html’)

def show(request):
if request.method==‘POST’:
x=request.POST.get(‘buy’)
print(x)

return render(request,'pages/shopshow.html', {"o":Books.objects.all()})

Is the name attribute for the variable in the button element “buy”?

And a side note - it’s three backticks - ` (not apostrophe - ' or quote - ") before and after the code, and the backticks must be a line by themselves.
Example:
The next line is ```

def function():
    return None
#The next line is ```

And it’s done.

i mean when i print(x) is printed none in cmd

it printed the word (buy) just like that none of any books info printed

Please post the current template.

{% csrf_token %}

{% for i in o %}

{{i.name}}

{{i.category}}

{% endfor %}

thats it

{% csrf_token %}

{% for i in o %}

{{i.name}}

{{i.category}}

{% endfor %}

i posted template but i dont know ehy after posting it doesnt show full code ? do u see it?