create a django project called openspaces that stores real estate listings, information and photos. Add functionality to create a user profile page, where the user can add/edit real estate listings and upload images
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Step 5: Create Views for Listings and User Profile (listings/views.py)
python
Step 6: Define Forms (listings/forms.py)
Create a new file forms.py in the listings app directory:
python
from django import forms
from .models import RealEstateListing, ListingImage
class ListingForm(forms.ModelForm):
class Meta:
model = RealEstateListing
fields = ('title', 'description', 'price', 'status')
labels = {
'title': 'Listing Title',
'description': 'Listing Description',
'price': 'Listing Price',
'status': 'Listing Status'
}
help_texts = {
'title': 'Enter a concise title for your listing.',
'description': 'Provide a detailed description of the property.',
'price': 'Enter the price of the property.',
'status': 'Select the current status of the listing.'
}
class ImageForm(forms.ModelForm):
class Meta:
model = ListingImage
fields = ('image',)
labels = {
'image': 'Upload Image'
}
help_texts = {
'image': 'Upload a clear photo of the property.'
}
In openspaces/urls.py, include the listings app URLs:
python
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('listings.urls')), # Include listings app URLs
path('accounts/', include('django.contrib.auth.urls')), # For login/logout
]
Step 8: Templates
Create a templates directory within your listings app, and within templates, create another listings directory to hold your templates. This structure is required for Django to automatically find your templates.
Example templates to create (minimal examples, expand as needed):
all_listings.html
create_listing.html
edit_listing.html
upload_image.html
listing_detail.html
user_profile.html
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import RealEstateListing, ListingImage
from .forms import ListingForm, ImageForm # We'll define these next
def all_listings(request):
try:
listings = RealEstateListing.objects.all()
return render(request, 'listings/all_listings.html', {'listings': listings})
except Exception as e:
messages.error(request, "Error fetching listings: " + str(e))
return render(request, 'listings/all_listings.html', {'listings': []})
@login_required
def create_listing(request):
if request.method == 'POST':
form = ListingForm(request.POST)
if form.is_valid():
listing = form.save(commit=False)
listing.user = request.user
listing.save()
messages.success(request, "Listing created successfully!")
return redirect('all_listings')
else:
messages.error(request, "Invalid form data. Please correct the errors.")
else:
form = ListingForm()
return render(request, 'listings/create_listing.html', {'form': form})
@login_required
def edit_listing(request, pk):
try:
listing = RealEstateListing.objects.get(pk=pk)
if listing.user != request.user:
messages.error(request, "You do not have permission to edit this listing.")
return redirect('all_listings')
if request.method == 'POST':
form = ListingForm(request.POST, instance=listing)
if form.is_valid():
form.save()
messages.success(request, "Listing updated successfully!")
return redirect('all_listings')
else:
messages.error(request, "Invalid form data. Please correct the errors.")
else:
form = ListingForm(instance=listing)
return render(request, 'listings/edit_listing.html', {'form': form})
except RealEstateListing.DoesNotExist:
messages.error(request, "Listing not found.")
return redirect('all_listings')
@login_required
def upload_image(request, pk):
try:
listing = RealEstateListing.objects.get(pk=pk)
if listing.user != request.user:
messages.error(request, "You do not have permission to upload images for this listing.")
return redirect('all_listings')
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
image = form.save(commit=False)
image.listing = listing
image.save()
messages.success(request, "Image uploaded successfully!")
return redirect('listing_detail', pk=pk)
else:
messages.error(request, "Invalid image. Please upload a valid image file.")
else:
form = ImageForm()
return render(request, 'listings/upload_image.html', {'form': form})
except RealEstateListing.DoesNotExist:
messages.error(request, "Listing not found.")
return redirect('all_listings')
def listing_detail(request, pk):
try:
listing = RealEstateListing.objects.get(pk=pk)
return render(request, 'listings/listing_detail.html', {'listing': listing})
except RealEstateListing.DoesNotExist:
messages.error(request, "Listing not found.")
return redirect('all_listings')
@login_required
def user_profile(request):
try:
listings = request.user.listings.all()
return render(request, 'listings/user_profile.html', {'listings': listings})
except Exception as e:
messages.error(request, "Error fetching user listings: " + str(e))
return render(request, 'listings/user_profile.html', {'listings': []})