У меня есть форма Django, которая отображает информацию об объекте недвижимости, и идея этой формы состоит в том, чтобы позволить пользователям изменять информацию об их свойствах. При отображении формы отображаются только данные изображения, все остальное пустое. От if request.user.landlord_profile.landlord_id == project.landlord_id:
- код формы.
views.py
def project_detail(request, pk):
project = Properties.objects.get(pk=pk)
applyButton = Property_Applications.objects.filter(listing=project)
propertyReview = Property_Reviews.objects.filter(property=project)
# getting the urls
property_images = Property_Images.objects.filter(property=project)
context = {
'project': project,
'propertyReview': propertyReview,
'property_images' : property_images,
}
if request.user.is_authenticated:
if request.user.last_name == 'False': # allows to tenant to view ads and apply for them if they meet the requirements
tenant_profile = Tenant_Profile.objects.get(tenant=request.user.tenant_profile.tenant_id)
if request.method == "POST":
applyButton = Property_Applications(
user=request.user,
listing=project,)
applyButton.save()
context['applyButton'] = applyButton
context['tenant_profile']= tenant_profile
if request.user.landlord_profile.landlord_id == project.landlord_id: # if the landlord owns this ad, this let's him edit the ad
change_listing_form = ManageListingForm(request.POST, request.FILES, instance=project)
if request.method == 'POST':
if change_listing_form.is_valid():
change_listing_form.landlord = request.user.landlord_profile
change_listing_form.save()
messages.success(request, f'Your account has been updated!')
else:
change_listing_form = ManageListingForm()
context['change_listing_form'] = change_listing_form
return render(request, 'project_detail.html', context)
forms.py - ManageListingForm
class ManageListingForm(forms.ModelForm):
class Meta:
model = Properties
fields = ['description','rentPrice','tenantSalary','referenceRequired','image']
project_detail. html - это это то, что отображает форму
{% elif request.user.landlord_profile.landlord_id == project.landlord_id%}
<p></p>
<div style="text-align: center">
<button class="open-button" onclick="openForm()">Manage your Spot</button>
</div>
<div class="form-popup" id="myForm">
<form class="form-container text-dark" method="POST" enctype="multipart/form-data">
<label for="Viewing Date " class="text-white">Enter a time that suits you to host a viewing with {{ portal.tenant }}</label>
<fieldset class="form-group col-md-12 bg-white border border-dark" style="margin: auto;padding: 10px">
{% csrf_token %}
{{change_listing_form | crispy}}
<div class="text-center">
<button type="submit" class="btn bg-dark text-white">Update your listing</button>
</div>
</fieldset>
<p></p>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</form>
<!-- Manage Listing function if the user is the owner-->
</div>
{% endif %}
Изображение формы только отображать данные изображения ![rm](https://i.stack.imgur.com/FTge6.png)