I've created a model for properties and connected it to a form. I started to render the form with template tags using {{ form.as_p }} and everything worked fine when I used my view for editing/updating an instance. But I wanted to customize my html so I manually rendered all form fields and now there is an issue when I try to update/edit a specific instance. The error says:
list_date
Enter a valid date/time
The thing is that I don't want the list_date to be able to be update by the user so I just have it as a hidden input (list_date should be the value as when the instance was created). Is it something in the html that is wrong? Can I solve this by handling it in my view funtion updateProperty?
models.py
from django.db import models
from django.utils import timezone
from accounts.models import User
class Property(models.Model):
realtor = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, default=User)
title = models.CharField(max_length=200)
property_type = models.CharField(max_length=200)
address = models.CharField(max_length=200)
area = models.CharField(max_length=200, blank=True)
city = models.CharField(max_length=200)
county = models.CharField(max_length=200, blank=True)
municipality = models.CharField(max_length=200, blank=True)
zipcode = models.CharField(max_length=200, blank=True)
floor = models.IntegerField(blank=True)
description = models.TextField(blank=True)
price = models.IntegerField()
rooms = models.IntegerField()
square_meter = models.IntegerField()
balcony = models.BooleanField(blank=True)
elevator = models.BooleanField(blank=True)
fee = models.IntegerField(blank=True)
photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
is_published = models.BooleanField(default=True)
list_date = models.DateTimeField(default=timezone.now, blank=True)
viewing_date = models.DateTimeField(timezone.now, null=True, blank=True)
viewing_date2 = models.DateTimeField(timezone.now, null=True, blank=True)
KOMMANDE = 'KO'
TILL_SALU = 'TS'
BUDGIVNING = 'BG'
SALD = 'SA'
PROPERTY_STATUS = [
(KOMMANDE, 'Kommande'),
(TILL_SALU, 'Till Salu'),
(BUDGIVNING, 'Budgivning'),
(SALD, 'Såld'),
]
property_status = models.CharField(
max_length=2,
choices=PROPERTY_STATUS,
default=KOMMANDE,
)
def __str__(self):
return self.title
forms.py
from django.forms import DateInput, ModelForm
from .models import Property
class PropertyForm(ModelForm):
class Meta:
model = Property
fields = '__all__'
widgets = {'list_date' : DateInput()}
views.py
from django.shortcuts import get_object_or_404, render, redirect
from .models import Property
from .forms import PropertyForm
from django.utils import timezone
def createProperty(request):
form = PropertyForm()
if request.method == "POST":
form = PropertyForm(request.POST)
print(request.POST)
if form.is_valid():
new_property = form.save(commit=False)
new_property.list_date = timezone.now()
new_property.realtor = request.user
new_property.save()
print(new_property.list_date)
return redirect('/properties/')
context = {'form':form}
return render(request, 'properties/property_form.html', context)
def updateProperty(request, pk):
property = Property.objects.get(id=pk)
form = PropertyForm(instance=property)
if request.method == "POST":
form = PropertyForm(request.POST, instance=property)
if form.is_valid():
updated_property = form.save(commit=False)
updated_property.save()
return redirect('/properties/')
context = {'form':form, 'property':property}
return render(request, 'properties/property_form.html', context)
property_form.html (only some parts)
<label for="{{ form.list_date.id_for_label }}" hidden></label>
<input type="hidden" name="{{ form.list_date.html_name }}" id="{{ form.list_date.id_for_label }}" {% if property %} value="{{ property.list_date }}" {% endif %}>
I want the list_date to be unchanged when editing and submitting the form.
I've tried to render the list_date in the hidden input as above. I'm very grateful for any solutions that will help me.