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.
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:
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 %}
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.
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.
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.
I tried .values object.values i tried brackets and paraenthesis.
I can't find it anywhere online.
i cant' find it in that link either.
yeah it's not there. It doesn't show anywhere where to just get the values.
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:
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!
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}"