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 %}