I've got a problem with DJ4E course by dr.Chuck. On Building Classified Ad Site #2 assignment my site can't pass a validation and checking form keeps throwing an error
"Could not find 'price' The price field is missing on the create form - check the field_list in views.py" for URL http://meredan.pythonanywhere.com/ads/ad/create I have re-checked multiple times and can't find why this is raised, because price field is available in the UI and also present when I checked manually in a shell. I am not very experienced with Django, maybe I'm missing something, would appreciate any help.
creation class in views.py
class AdCreateView(LoginRequiredMixin, View):
template_name = 'ads/ad_form.html'
success_url = reverse_lazy('ads:all')
def get(self, request, pk=None) :
form = CreateForm()
ctx = { 'form': form }
return render(request, self.template_name, ctx)
def post(self, request, pk=None) :
form = CreateForm(request.POST, request.FILES or None)
if not form.is_valid() :
ctx = {'form' : form}
return render(request, self.template_name, ctx)
# Add owner to the model before saving
ad = form.save(commit=False)
ad.owner = self.request.user
ad.save()
return redirect(self.success_url)
forms.py
class CreateForm(forms.ModelForm):
max_upload_limit = 2 * 1024 * 1024
max_upload_limit_text = naturalsize(max_upload_limit)
picture = forms.FileField(required=False, label='File to Upload <= '+max_upload_limit_text)
upload_field_name = 'picture'
class Meta:
model = Ad
fields = ['title', 'text', 'price', 'picture']
# Validate the size of the picture
def clean(self) :
cleaned_data = super().clean()
ad = cleaned_data.get('picture')
if ad is None : return
if len(ad) > self.max_upload_limit:
self.add_error('picture', "File must be < "+self.max_upload_limit_text+" bytes")
# Convert uploaded File object to a picture
def save(self, commit=True) :
instance = super(CreateForm, self).save(commit=False)
# We only need to adjust picture if it is a freshly uploaded file
f = instance.picture # Make a copy
if isinstance(f, InMemoryUploadedFile): # Extract data from the form to the model
bytearr = f.read();
instance.content_type = f.content_type
instance.picture = bytearr # Overwrite with the actual image data
if commit:
instance.save()
return instance
class CommentForm(forms.Form):
comment = forms.CharField(required=True, max_length=500, min_length=3, strip=True)
ad_form.html
{% extends 'base_menu.html' %}
{% block title %}{{ settings.APP_NAME }}{% endblock %}
{% load crispy_forms_tags %}
{% block content %}
<p>
<form action="" method="post" id="upload_form" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary ">
<input type="submit" value="Cancel" class="btn btn-default" onclick="window.location.href='{% url 'ads:all' %}';return false;">
</div>
</form>
</p>
<script>
$("#upload_form").submit(function() {
console.log('Checking file size');
if (window.File && window.FileReader && window.FileList && window.Blob) {
var file = $('#id_{{ form.upload_field_name }}')[0].files[0];
if (file && file.size > {{ form.max_upload_limit }} ) {
alert("File " + file.name + " of type " + file.type + " must be < {{ form.max_upload_limit_text }}");
return false;
}
}
});
</script>
{% endblock %}
Manually checking In [2]: CreateForm.Meta.fields Out[2]: ['title', 'text', 'price', 'picture']
Also tried cleaning cache of browser, Django cache, made migrations, disabled extensions