Tuesday, 4 August 2020

1. ModuleNotFoundError: No module named 'pip._internal.cli.main'

When changing my IDE halfway through a project I got this error message when trying to 'runserver' in Terminal.

'ModuleNotFoundError: No module named 'wagtailmenus''

So I tried 
'pip install wagtailmenus' 

And received the following message. 

'
ModuleNotFoundError: No module named 'pip._internal.cli.main''


These are the commands I used to sort this out. 

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py --force-reinstall pip uninstall pip python3 get-pip.py
pip install wagtailmenus
Now I can

python3 manage.py runserver

And my site is now working locally .

AWS lightsail for Django and Wagtail project.

I was looking for somewhere to host my code so that I could use it from wherever I want.  Just for myself at this stage and as AWS were offering a free month.   So I thought I’d review it here.
Unfortunately though I didn’t get to the point I wanted to with it , so have now ditched it.   I think it’d be worth other developers considering my issues and finding resolutions for these before you end up wasting your time as well.
https://aws.amazon.com/getting-started/hands-on/deploy-python-application/
and the first steps here.  
https://www.youtube.com/watch?v=-rO67fTzVBc

The initial part of setting up a Django project on Lightsail was really straightforward, using the instructions here. 

1. Create a Lightsail instance

2. Deploy a project - I deployed my own custom Django code here. 

3. Using the ‘connect SSH’ I could git clone the project I was working on from Github

4.However for the moment I had to disable everything ‘Wagtail’ in my project to get it working. 

5. In the instruction above and following ‘Test the application’    


At this point I can get a website that I can see and access from anywhere using


‘cd /opt/bitnami/apps/django/django_projects/django_sb_goals/ && python3 manage.py runserver 0.0.0.0:8000”


However only while I have the SSH window open on my computer, once I shut this down it stops working.


The solution for which I was hoping would be by following the instructions ‘Host the application using Apache ‘ .  I reviewed these instructions a number of times and could not get them to work.


I also spent the best part of a working day Googling and searching for a solution .  With no success.


The other problem - Getting Wagtail to work .  As I could not ‘pip install wagtail’ I would have needed to change a fair bit of code.  Considering this and the first issue, and that I’d already wasted a lot of Dev time I decided to abandon using AWS lightsail as my hosting solution.


I’d be interested to find out the resolutions to these issues if anyone knows why I struggled, or other hosting recommendations. 


Thursday, 16 July 2020

Using Wagtailmenus and solution to adding Flat Menus as a dropdown item to the main menu.

n my previous post I gave a example of a template for a ‘main_menu’ in wagtail menus.
https://www.blogger.com/blog/post/edit/preview/8608610523928821757/2576531812455589045

My next task was in Wagtailmenus how to display custom flat menus as dropdowns. And so it seems it wasn’t that straightforward. In hindsight may be solved by better planning and partly due to the fact I’d taken a Django site and made it into a Wagtail CMS driven one. Meaning that not all APP pages are created and accessible through the Wagtail CMS - Ideally they would be. In which case you could use the ‘Allow sub-menu for this item’ tick box that would automatically generate the menu items.

So anyway I thought it’d be handy to be able to have ‘Flat menus’ that I could add to the Main Menu, and I’ve come up with a way to facilitate that. It feels a little hacky in areas, so feel free to contribute any changes that you think would help. I’ve added the code to Github .


In my Main Menu template I use 'item-handle' to check against an 'if' statement to switch the menu I'm showing. I would need to do this but in the call up {% flat_menu 'sub-folder' %} I can't use {% flat_menu 'sub-{{ item.handle }}' %}

Which would make my code look a lot better.

In base.html

{% main_menu max_levels=3 template="menus/custom_main_menu.html" %}


in menus/custom_main_menu.html

{% load menu_tags %}
{% if menu_items %}
<nav class="nav-main">
<ul>
{% for item in menu_items %}
<li class="menu-item">
{% if item.handle == 'folder' %}
<a href="#" data-nav-primary-submenu-trigger="" class="submenu-trigger icon icon-{{ item.handle }}">{{ item.text }}</a>
<div class="nav-submenu">
<h2 id="nav-submenu-quiver-title" class="icon icon-{{ item.handle }}">{{ item.text }}</h2>
{% flat_menu 'sub-folder' template="menus/custom_sub_menu.html" %}
</div>
{% elif item.handle == 'date' %}
<a href="#" data-nav-primary-submenu-trigger="" class="submenu-trigger icon icon-{{ item.handle }}">{{ item.text }}</a>
<div class="nav-submenu">
<h2 id="nav-submenu-calendar-title" class="icon icon-{{ item.handle }}">{{ item.text }}</h2>
{% flat_menu 'sub-date' template="menus/custom_sub_menu.html" %}
</div>
{% else %}
<a href="{{ item.href }}" class="icon icon-{{ item.handle }}">{{ item.text }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
</nav>
{% endif %}


in custom_sub_menu.html



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



This is available at https://github.com/deejayM/flat_menu_dropdown_example .  Please help me improve this if you can, it's meant as a quick fix to my issue and not a full solution but thought it may help someone else looking to solve the same problem. 

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.