Hi I haven’t used Django in years and cannot work out the error I’m getting, I think I need a new url path but please someone correct my otherwise.
URLS
from django.urls import path
from .views import PropertyListView
app_name = 'listings'
urlpatterns = [
path('property-list/', PropertyListView.as_view(), name='property-list'),
]
VIEW
class PropertyListView(View):
def get(self, request):
form = PropertyFilterForm()
context = {
'form': form,
}
return render(request, 'listings/property-list.html', context)
def post(self, request):
form = PropertyFilterForm(request.POST)
if form.is_valid():
start_date = form.cleaned_data['start_date']
end_date = form.cleaned_data['end_date']
suburb = form.cleaned_data['suburb']
state = form.cleaned_data['state']
postcode = form.cleaned_data['postcode']
listings = PropertyListing.objects.all().order_by('-date_sold')[:10]
if start_date and end_date:
listings = listings.filter(date_sold__range=[start_date, end_date])
if suburb:
listings = listings.filter(suburb__icontains=suburb)
if state:
listings = listings.filter(state__icontains=state)
if postcode:
listings = listings.filter(postcode__icontains=postcode)
context = {
'form': form,
'listings': listings,
}
return render(request, 'listings/property-list.html', context)
# Form is invalid, render the form again
context = {
'form': form,
}
return render(request, 'listings/property-list.html', context)
FORM
class PropertyFilterForm(forms.Form):
start_date = forms.DateField(label='Start Date', required=False, input_formats=['%Y-%m-%d'], widget=forms.DateInput(attrs={'type': 'date', 'placeholder': 'YYYY-MM-DD'}))
end_date = forms.DateField(label='End Date', required=False, input_formats=['%Y-%m-%d'], widget=forms.DateInput(attrs={'type': 'date', 'placeholder': 'YYYY-MM-DD'}))
suburb = forms.ChoiceField(label='Suburb', choices=[], required=False)
state = forms.ChoiceField(label='State', choices=[], required=False)
postcode = forms.ChoiceField(label='Postcode', choices=[], required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['start_date'].choices = self.get_date_choices()
self.fields['end_date'].choices = self.get_date_choices()
self.fields['suburb'].choices = self.get_suburb_choices()
self.fields['state'].choices = self.get_state_choices()
self.fields['postcode'].choices = self.get_postcode_choices()
def get_date_choices(self):
return [(date_sold, date_sold) for date_sold in PropertyListing.objects.values_list('date_sold', flat=True).distinct()]
def get_suburb_choices(self):
return [(suburb, suburb) for suburb in PropertyListing.objects.values_list('suburb', flat=True).distinct()]
def get_state_choices(self):
states = PropertyListing.objects.values_list('state', flat=True).distinct()
return [(state, state) for state in states]
def get_postcode_choices(self):
return [(postcode, postcode) for postcode in PropertyListing.objects.values_list('postcode', flat=True).distinct()]
HTML
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous">
</script>
{% block head %}
<title>
Base
</title>
{% endblock %}
</head>
<head>
<title>Property Listings</title>
</head>
<body>
<h1>Property Listings</h1>
<form method="post" action="{% url 'property-list' %}">
{{ form.as_p }}
<button type="submit">Filter</button>
</form>
{% if form.is_valid %}
<table>
<thead>
<tr>
<th>ID</th>
<th>Date Sold</th>
<th>Sold By</th>
<th>Price Guide</th>
<th>Price Guide Info</th>
<th>Address</th>
<th>Number of Beds</th>
<th>Number of Baths</th>
<th>Number of Parking</th>
<th>Size (m²)</th>
<th>House Type</th>
<th>Inspection</th>
<th>Full Address</th>
<th>Listing Type</th>
<th>Suburb</th>
<th>State</th>
<th>Postcode</th>
<th>Profile</th>
</tr>
</thead>
<tbody>
{% for listing in listings %}
<tr>
<td>{{ listing.id }}</td>
<td>{{ listing.date_sold }}</td>
<td>{{ listing.sold_by }}</td>
<td>{{ listing.price_guide }}</td>
<td>{{ listing.price_guide_info }}</td>
<td>{{ listing.address1 }}</td>
<td>{{ listing.num_of_beds }}</td>
<td>{{ listing.num_of_baths }}</td>
<td>{{ listing.num_of_parking }}</td>
<td>{{ listing.size_msq }}</td>
<td>{{ listing.house_type }}</td>
<td>{{ listing.inspection }}</td>
<td>{{ listing.full_address }}</td>
<td>{{ listing.listing_type }}</td>
<td>{{ listing.suburb }}</td>
<td>{{ listing.state }}</td>
<td>{{ listing.postcode }}</td>
<td>{{ listing.profile }}</td>
</tr>
{% empty %}
<tr>
<td colspan="18">No listings available.</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</body>
</html>
Exception
Request Method: GET
Request URL: http://127.0.0.1:8000/listings/property-list/?start_date=2022-01-01&end_date=2023-06-07&suburb=DYERS+CROSSING&state=NSW&postcode=2429
Django Version: 4.2.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'property-list' not found. 'property-list' is not a valid view function or pattern name.