User Dashboard Not Showing Inquries

I’m making a real estate website that allows logged in users to see if they’ve made any inquiries on their dashboard page, which should appear as a table. If there’s been no activity then a message will say that. So far I’ve made some inquiries and it has gone to the database as it’s supposed to but the dashboard hasn’t updated correctly. Thanks

contact - views.py:

from django.shortcuts import render, redirect
from .models import Contact
from django.contrib import messages

def contact(request):
    if request.method == 'POST':
        listing_id = request.POST['listing_id']
        listing = request.POST['listing']
        name = request.POST['name']
        email = request.POST['email']
        phone = request.POST['phone']
        message = request.POST['message']
        user_id = request.POST['user_id']
        estate_agent_email = request.POST['estate_agent_email']

        # check if user has made an inquiry
        if request.user.is_authenticated:
            user_id = request.user.id
            has_contacted = Contact.objects.all().filter(listing_id=listing_id, user_id=user_id)
            if has_contacted:
                messages.error(request, 'You have already inquired about this listing')
                return redirect('/listings/'+listing_id)

        contact = Contact(listing=listing, listing_id=listing_id, name=name, email=email, phone=phone, message=message, user_id=user_id)
        contact.save()

        messages.success(request, 'Your request has been submitted')

        return redirect('/listings/'+listing_id)

contact - models.py:

from django.db import models
from datetime import datetime


class Contact(models.Model):
    listing = models.CharField(max_length=100)
    listing_id = models.IntegerField()
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=50)
    phone = models.CharField(max_length=50)
    message = models.TextField(blank=True)
    contact_date = models.DateTimeField(default=datetime.now, blank=True)
    user_id = models.IntegerField(blank=True)

    def __str__(self):
        return self.name

contact - urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('contact', views.contact, name='contact')
]

dashboard.html

{% extends 'base.html' %}
{% block title %} | Dashboard {% endblock %}
{% block content %}
<section id="showcase-inner" class="py-5 text-white">
    <div class="container">
        <div class="row text-center">
            <div class="col-md-12">
                <h1 class="display-4">User Dashboard</h1>
                <p class="lead">Manage your Bridge Property account</p>
            </div>
        </div>
    </div>
</section>

<!-- Breadcrumb -->
<section id="bc" class="mt-3">
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="{% url 'index' %}">
                        <i class="fas fa-home"></i> Home</a>
                </li>
                <li class="breadcrumb-item active"> Dashboard</li>
            </ol>
        </nav>
    </div>
</section>

<!-- Alerts -->
{% include 'includes/messages.html' %}

<section id="dashboard" class="py-4">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h2>Welcome {{ user.first_name }}</h2>
                {% if contact %}
                    <p>Here are the property listings that you have inquired about</p>
                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">#</th>
                                <th scope="col">Property</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for con in contact %}
                            <tr>
                                <td>{{ contact.id }}</td>
                                <td>{{ contact.listing }}</td>
                                <td>
                                    <a class="btn btn-light" href="{% url 'listing' listing_id %}">View Listing</a>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                {% else %}
                    <p>There are no inquiries</p>
                {% endif %}    
            </div>
        </div>
    </div>
</section>
{% endblock %}

Please post the view that renders and displays the dashboard.

Also post the view and form that post to the contact view.

You have:

What happens if you run this query from the shell with the listing_id and user_id that you are testing with? Does it return the Contact object you’re expecting to see?

Thanks for the reply Ken, my views.py file for the dashboard:

def dashboard(request):
    user_contact = Contact.objects.order_by('-contact_date').filter(user_id=request.user.id)
    context = {
        'user_contact': user_contact
    }
    return render(request, 'account/dashboard.html', context)

The contact view is supposed to be registered here:

<!-- Inquiry Modal -->
<div class="modal fade" id="inquiryModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="inquiryModalLabel"><em>Make An Inquiry</em></h3>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="{% url 'contact' %}" method="POST">
                    {% csrf_token %}
                    {% if user.is_authenticated %}
                        <input type="hidden" name="user_id" value="{{ user.id }}">
                    {% else %}
                        <input type="hidden" name="user_id" value="0">
                    {% endif %}
                    <input type="hidden" name="estate_agent_email" value="{{ listing.estate_agent.email }}">
                    <input type="hidden" name="listing_id" value="{{ listing.id }}">
                    <div class="form-group">
                        <label for="property_name" class="col-form-label">Property:</label>
                        <input type="text" name="listing" class="form-control" value="{{ listing.address }}">
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-form-label">Name:</label>
                        <input type="text" name="name" class="form-control" {% if user.is_authenticated %}
                        value="{{ user.first_name }} {{ user.last_name }}" {% endif %} required>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-form-label">Email:</label>
                        <input type="email" name="email" class="form-control" {% if user.is_authenticated %}
                        value="{{ user.email }}" {% endif %} required>
                    </div>
                    <div class="form-group">
                        <label for="phone" class="col-form-label">Phone:</label>
                        <input type="text" name="phone" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="message" class="col-form-label">Message:</label>
                        <textarea name="message" class="form-control"></textarea>
                    </div>
                    <hr>
                    <input type="submit" value="Send" class="btn btn-block btn-secondary info-btn">
                </form>
            </div>
        </div>
    </div>
</div>

When I ran that query I got the following output:
bash: syntax error near unexpected token `listing_id=listing_id,’
Wasnt expecting to see that output anyways

That needs to be run in the Django shell, and you need to replace the variable listing_id with a value of a listing id that has been used as a contact.

I ran that line in the shell and got this error:

NameError                                 Traceback (most recent call last)
Cell In[1], line 1
----> 1 Contact.objects.filter(listing_id=13, user_id=user_id)

NameError: name 'Contact' is not defined

If Contact isn’t defined how would it output the message saying there’s been no inquries?

How are you running the shell? If you’re not used to running the Django shell, review the docs at Playing with the API.

(Have you worked your way through the Official Django Tutorial?)

I’ve got a template that I’m running through codeanywhere and I’ve only briefly looked through those tutorials

Hopefully they have a way to let you use the Django shell. Either that, or connect to the database and directly verify that the data you’re expecting to see is actually there.

You’ll want to work your way through the official tutorial. You’ve got some code that you didn’t need to write, because Django can do that work for you.

The info that is submitted through the form is in the database but yea I’ll take a look at the docs and see if it’ll help

Also, in your dashboard view you have:

But, in your dashboard template you have:

and

and then…

Your template isn’t referencing the variable in the context, and you’re not referencing the iteration variable within your template.

Hey Ken, I eventually saw that so it’s working now, cheers

It appears that while you’ve set up the backend logic for users to make inquiries on listings, the frontend dashboard isn’t reflecting these inquiries correctly. To resolve this, ensure that you’re querying the database correctly to fetch a user’s inquiries and passing this data to the dashboard template. In your views.py, you’ll need a dedicated view function for the dashboard that retrieves the user’s inquiries using their user_id and then passes this data to the dashboard.html template. Once you’ve updated the view, ensure that the template iterates over the fetched inquiries using the correct loop variable, which should be con rather than contact. This adjustment should enable the dashboard to display the user’s inquiries or show a message if there are none.