Friday 29 May 2020

Wagtail: 'no such table' fix

When following the tutorial on setting up a blog in Wagtail I was getting the following issue when trying to add my Child blog page.

"no such table 'blog_blogindexpage' "

I'm not sure why but I couldn't fix just by changing my Models.py .  It didn't seem to be reading it all .  So I had to do the following 

1. Delete all my migrations in the blog/migrations table. 
2. Make sure the Class  BlogIndexPage 
 is in my 'models.py'
3. Run the following commands 

python manage.py makemigrations blog
python manage.py migrate

Wednesday 27 May 2020

Django - Can't get Images to upload in custom Class based form




In my models.py I have the following

class Board(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField(default='default-sb.jpg', upload_to='surfboard_pics')

The field is called in the Class based form 

class BoardCreateView(LoginRequiredMixin, CreateView):
model = Board
fields = ['title', 'image', 'length', 'width', 'volume', 'wave_range_start',
'wave_range_start', 'wave_range_end', 'shaper', 'year', 'make', 'notes']

At this point I can see the field.  The image upload works in Admin but not in my Form !



The fix was this .  In the template you need 'multipart enctype' to the Form tag.  Like this 

<form method="POST" enctype="multipart/form-data">

After adding this my form now works. 











Django - Make field not required

The solution for this was fairly simple but I had to search for a fair bit.  

Hopefully this will help someone else out. 

In your 'model.py' of the App you're working on you need to make this one simple change. 

I had 

width = models.CharField(max_length=5)


Which I added 

width = models.CharField(max_length=5, null=True, blank=True)

You'll also need to run the migrates again .  So 

python manage.py makemigrations YOURAPPNAME


Python manage.py migrate


You should now see that there's no asterix against this field on the form and that you can save a Null value.