Tuesday, 14 July 2020

WagtailMenu template example

I found it difficult to find an example of a Menu template example for WagtailMenus -> https://wagtailmenus.readthedocs.io/en/stable/


The 'menus' folder will go in the 'templates' folder of your app.  

This is a very simple example and does not include a Sub Menu .  Which will follow. 

{% load menu_tags %}
{% if menu_items %}
<nav class="nav-main">
<ul>
{% for item in menu_items %}
<li class="menu-item">
<a href="{{ item.href }}" class="icon {{ item.handle }}">{{ item.text }}</a>
</li>
{% endfor %}
</ul>
</nav>
{% endif %}

I've used 'handle' to store the class name I want, but this may not be suitable for you. 


Wednesday, 24 June 2020

In Django Shell I can see the Object but I can't Access it.

A simple one this but I couldn't find the answer with a Google search so I thought I'd list it here. 


What I have is a Profile object that links to my User object.  And I'm trying to get the 'image' data from it. 

Using Shell this is what I was doing 

python manage.py shell

from users.models import Profile
profile = Profile.objects.filter(user=1)
profile.image

Which would return me the message.

 'QuerySet' object has no attribute 'image'

However I know its there because when I run


profile.values() 

I can see it.  

The answer is straight forward and you need to do the following to call the specific item that you need. 

profile = Profile.objects.filter(user=1).first()
You'll then be able to user 'profile.image' to return the data.


Wednesday, 17 June 2020

django.db.utils.IntegrityError: The row in table ‘%’ with primary key '1' has an invalid foreign key: %.user_id contains a value '1' that does not have a corresponding value in auth_user.id.

This is on a Django / Wagtail project.  Although I think the issue is relevant only to the Django side of it here. 

I was getting this error when running some tests on my APP .

python3 manage.py test tasks



The error :
django.db.utils.IntegrityError: The row in table ‘%’ with primary key '1' has an invalid foreign key: %.user_id contains a value '1' that does not have a corresponding value in auth_user.id.


The offending code :


django models.ForeignKey(User, on_delete=models.CASCADE, default=1)


Here's what I searched when looking for an answer:

Django how to add a default user id to model


Django model foreignKey user pass in default user



Django does not have a corresponding value in auth_user.id


I tried a few different methods to resolve including removing all my migration file and running the migrate commands again .


python3 manage.py makemigrations


Python3 manage.py migrate



What I found worked though was to change my code to the following line ( and then run the above migration commands again )



user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)

Wednesday, 3 June 2020

Wagtail: admin KeyError at /admin/

I recently saw this error after playing around with Page Model Classes.   

To fix this I worked out which Class was at fault and had to remove it and all mentions of it in the 'Migrations' folder.  

Once I did that I could run the following commands again and had regained access to my /admin/ folder 

python manage.py makemigrations
python manage.py migrate
python manage.py runserver

I also renamed my Class to not cause any other possible conflicts. 

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.