Python Forum
Working on an Auction Site - had questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working on an Auction Site - had questions
#11
Which database are you using? SQLite and PostgreSQL certainly have command line tools that will let you connect and make SQL queries; other databases might also. Or, you can use a third party tool like DBeaver. I'm assuming a relational database here, of course.

Obviously if you want to do something in the app, just make a query to get the data out. Looks like you're using Django's ORM, so see the docs to see how to query.
Reply
#12
Yes I'm using Django ORM.

OK so I have it printing out the objects on my Auction page. But it's just the names of the objects and not the actual data. How do I change this? I tried the .values method but it didn't work saying there was no values method.

Here's what I'm seeing on the page:
[Image: v.php?i=7bca5f3445]

Code:

views.py :
def auction(request):
    if request.method == "POST":
        title = request.POST.get("title")
        description = request.POST.get("description")
        price = request.POST.get("price")
        category = request.POST.get("category")
        new = NewPost.objects.create(title = title, description = description, price = price, category = category)
        new.save()
        return render(request, "auctions/auction.html")
    else:
        query = NewPost.objects.all()
        print(query)
        return render(request, "auctions/auction.html", { "queries": query })
auction.html :

{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Post Your Item For Sale</h2>
    
    <form
       name="auction"
       action="/auction"
       method="post"     >{% csrf_token %}
                <input type="text" name="title" placeholder="Enter Title of Item" required>
                <input type="textarea" size=100 name="description" placeholder="Enter Description of Item" required>
                <input type="number" name="price" placeholder="Enter Starting Price" required>
                <input type="text" name="category" placeholder="Enter Category" required>
                <input type="submit" id="auction" name="auction" value="Create New Auction" />
         </form>   
{{% for query in queries %}
            <li><a href="/wiki/{{entry}}">{{ query }}</a></li>
{% endfor %}  
{% endblock %}
Reply
#13
The image is broken. In any case, I suspect you're getting exactly what you asked for - on line 17, you're rendering the object as a whole, rather than the individual fields.
Reply
#14
[Image: Jq5b2zM.png]

there's a better link. Do you know the syntax to get the individual fields? I want all the fields of each entry. I looked everywhere and cant' find it.
Reply
#15
Don't you just use the dot operator? I don't know where you looked, but I suspect you didn't do a good job. Just looking through the table of contents in the docs, I found a page on the template system.
Reply
#16
I tried .values object.values i tried brackets and paraenthesis.

I can't find it anywhere online.
Reply
#17
i cant' find it in that link either.
Reply
#18
yeah it's not there. It doesn't show anywhere where to just get the values.
Reply
#19
I want to print out the actual data instead of NewPost object (1), etc. How do I print the actual data of the QuerySet instead of just the NewPost object (1)?

Here is what I'm printing out now:

[Image: JqcfQkS.png]

Here is my code:

views.py (just the auction part):

def auction(request):
    if request.method == "POST":
        title = request.POST.get("title")
        description = request.POST.get("description")
        price = request.POST.get("price")
        category = request.POST.get("category")
        new = NewPost.objects.create(title = title, description = description, price = price, category = category)
        new.save()
        return render(request, "auctions/auction.html")
    else:
        query = NewPost.objects.all()
        print(NewPost.title)
        return render(request, "auctions/auction.html", { "queries": query })
models.py:

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    pass

class NewPost(models.Model):
    title = models.CharField(max_length=64)
    description = models.CharField(max_length=64)
    price = models.IntegerField()
    category = models.CharField(max_length=64)

class Bid():
    pass

class Comment():
    pass
auction.html:

{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Post Your Item For Sale</h2>
    
    <form
       name="auction"
       action="/auction"
       method="post"     >{% csrf_token %}
                <input type="text" name="title" placeholder="Enter Title of Item" required>
                <input type="textarea" size=100 name="description" placeholder="Enter Description of Item" required>
                <input type="number" name="price" placeholder="Enter Starting Price" required>
                <input type="text" name="category" placeholder="Enter Category" required>
                <input type="submit" id="auction" name="auction" value="Create New Auction" />
         </form>   
{{% for query in queries %}
            <p>{{ query }}<p>
{% endfor %}  
{% endblock %}
Thanks!
Reply
#20
So the main way, would be to actually use each field you want to display within the template:
{{% for query in queries %}
            <p>{{ query.title }} - {{ query.description }} - {{ query.price }} - {{ query.category }}<p>
{% endfor %}  
If you don't want to do that, you can try (I haven't actually tested this, I'm just guessing it'll work) using the __str__() method to describe what your object looks like as a string:
class NewPost(models.Model):
    title = models.CharField(max_length=64)
    description = models.CharField(max_length=64)
    price = models.IntegerField()
    category = models.CharField(max_length=64)

    def __str__(self):
        return f"{self.title} - {self.description} - {self.price} - {self.category}"
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020