Python Forum
Posting a value of a dictionary with only one value in HTML
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Posting a value of a dictionary with only one value in HTML
#1
Hey guys I'm trying to pass a value through the render method and it only accepts dictionaries. But I only want it to get one dataset. So I'm doing it by searching through data with the the same "title". I have it only grabbing the first dataset using the first() method.

However, now I can't iterate through it since it's only one dataset.

If I try to just user query.title - it shows nothing - I get no error though - it just doesn't show the title on the HTML page.

If I try to iterate though it, it gives me an error that the object is not iterable.

How can I display the one dataset result that I got from my query in HTML?

Here is my code:

views.py:

def post(request, title, description, price, category):
    title = title
    description = description
    price = price
    category = category
    query = NewPost.objects.filter(title = title).first()
    return render(request, "auctions/post.html",  { "queries": query })
post.html :

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

{% block body %}
{% for query in queries %}
    <h2>{{ query.title}}</h2>
{% endfor %}  

    
{% endblock %}
Reply
#2
There is first template filter.
So, if you have a queryset queries, you can do in your template:

{% extends "auctions/layout.html" %}
 
{% block body %}
{% for query in queries %}
    <h2>{{ query.title}}</h2>
{% endfor %}  
{% queries | first %} 
     
{% endblock %}
You don't need to call .first() method in your view in this case.
Reply
#3
Thanks - I got the first working well now.

But now i'm noticing it's the ones that have a title, description or category that starts with a lowercase letter...so I can fix that. But some of them say NoneType has no attribute "title". which I'm guess is for some reason coming up with None for the title for a certain few for some reason. Anyone know why i'm getting a None for the title on certain items?

Reply
#4
(Sep-10-2020, 04:43 AM)card51shor Wrote: But some of them say NoneType has no attribute "title". which I'm guess is for some reason coming up with None for the title for a certain few for some reason.

No, it means the thing you're trying to access the title attribute of is itself None - that is, query is None.
Reply
#5
but why would it be none for some but not others?
Reply
#6
Hint: where does that value come from?

You really do need to learn to work backwards to work out what the source of the problem is.

Also, you really should show the updated code after you change it. Saves us having to guess.
Reply
#7
A lot of the time I do find the source of the problem - when I don't I come to you guys to help me. I can't find it.

The value comes from the title
Reply
#8
(Sep-12-2020, 07:30 PM)card51shor Wrote: The value comes from the title

Sigh, if the error message was AttributeError: 'NoneType' object has no attribute 'title', then as above, it's the object on which you're trying to access title on that's the problem. That is, it's your query object. Where does that come from?
Reply


Forum Jump:

User Panel Messages

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