django views issue?

thats it if code doesnt show

You need to surround the template between lines of three backtick characters.

You need a line of ```
Then the template
Then another line of ```

pls look at photo i dont have battery remaining

‘’

    <title> this is show page</title>
    <style>
        body{
            background-color: indigo;
        }
        
    </style>
</head>



{% csrf_token %}

{% for i in o %}

{{i.name}}

{{i.category}}

{% endfor %}

‘’

i pasted the whole code but it doesnt show completely i used ‘’’ then template then ‘’’ but same i dont know how it works when i create this topic

It’s three backticks - ` (not apostrophe - ’ or quote - ") before and after the code.

You’re showing that you’re trying to use apostrophe - ' or possibly even “smart quotes” like you might get from a word processor.

<!DOCTYPE html>
<html>
    <head>

        <title> this is show page</title>
        <style>
            body{
                background-color: indigo;
            }
            
        </style>
    </head>
<body align="center">
    <br><br><br>
    <form method="post">
        {% csrf_token %}

{% 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="save" name="buy">


</div>
{% endfor %}


    
</div>
    </form>

</body>

</html>

Good, now review the previous responses:

u see i just named the button “buy” now i have only book in database with primary key=1
see pic below

just wanna know how to send the book info to views ?
this is views code


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

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

if the value of input syntax like
input type=“submit” value=“save” name=“buy”>

where do i put the primary key?

Hi,

I suggest you make the following changes to your code so that it will be less confusing for you … and those trying to help you.

Go to your view. Change "o" to "books". That name makes more sense. Therefore, your show view may look like this:

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

Now change your template too. Consider this version:

!DOCTYPE html>
<html>
    <head>
        <title> this is show page</title>
        <style>
            body{
                background-color: indigo;
            }
        </style>
    </head>
    <body align="center">
        <br><br><br>
        <form method="post">
            {% csrf_token %}
            <!-- pay attention to the next line -->
            {% for book in books %}
                <div style="display: inline-block; border-style: solid; border: 2px;">
                    <img src="{{book.photo.url}}" style="height: 100px; width: 100px;"/>
                    <p  style="font-size: larger;">{{book.name}}</p>
                    <p style="font-size:large;">{{book.category}}</p>
                    <!-- use the book id as value next -->
                    <input type="submit" value="{{ book.id }}" name="buy">
                </div>
            {% endfor %}
            </div>
        </form>
    </body>
</html>

You can copy the above version and try it. I put in comments to explain some lines. Notice that I set the value to the book.id – I suspect this is what you want. This will print the book’s id when you press the buy button

1 Like