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')
]
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?
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.
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?
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.
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.