Monday, 30 November 2020

Django restricting a Class Based View page depending on a User attribute / Group etc.

The following code is an example of how you can make it so users get redirected depending on conditions taken from their User model . 


This code is to go in a Class Based View. 

1 2 3 4 def dispatch(self, request, *args, **kwargs): if request.user.username == 'david.smith': return redirect('home_page') return super().dispatch(request, *args, **kwargs)



This example would redirect the user 'david.smith'.  This next example would redirect if the User does not belong to the group that we want. 


def dispatch(self, request, *args, **kwargs): if request.user.groups.filter(name = 'Coordinator').exists(): return super().dispatch(request, *args, **kwargs) return redirect('home_page')


Monday, 23 November 2020

PythonAnywhere Internal Server Error ager '/favicon.ico' call added.

 After adding my Favicon pack to the Static files in PythonAnywhere ( using the 'Files' link; and then adding the files in mysite/static folder ) 
And then when accessing the site the next time I was seeing the following message on the homepage. 


Internal Server Error


On checking the error.log ( go to > Webb > Scroll down to 'Error.log' ) 
I could see the message ' Internal Server Error: /favicon.ico' 


So my next port of call was to check that the favicon.ico could be seen at the same URL ( relatively ) as the one that I have working on my local server.  It was working though, so no issue there. 


So my next port of call was to check that the favicon.ico could be seen at the same URL ( relatively ) as the one that I have working on my local server.  It was working though, so no issue there. 

After revisiting the issue I realised it was the message above the 'Internal Server Error' that was most useful . 


raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

ValueError: Missing staticfiles manifest entry for 'images/favicon.ico'

2020-11-23 14:39:05,951: Internal Server Error: /favicon.ico


Missing staticfiles manifest entry in Django issue


Which led me to adding the following change which worked. 

OPEN 
/mysite/settings/base.py

FIND

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'


and changed this to . 

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'



It might not be recommended but for the small site I'm working on this fix will be fine.  And felt it's worth noting for anyone else who stumbles over the same issue. 

Monday, 16 November 2020

Visual Studio Code - "go to definition" not working .

 This was happening on VSC "Version: 1.51.1" 

For example if I was to create the following test.py code 

<code>

def my_function():
  print("Hello from a function")

my_function()

</code>

Then I should be able to right click 'my_function' and get an option 'go to definition' which will take to to the above function.  Unfortunately I was getting a 'no definition' available message. 

To fix this open VSC . 

use Command + Shift + P ( Mac ) 








Search for 'settings.json' 

And add this to your code 

<code>

{
// Defines type of the language server.
"python.jediEnabled": false,
"python.languageServer": "Microsoft"
}

<code> 

Restart VSC .  This then resolved my issue for the code example given above.

 
However I discovered another issue 'Visual Studio Code unresolved import python ' 

Which I would get when clicking on imported code , for example if I clicked on 'selenium' here. 

from selenium import webdriver

The solution for me here was to click on 'Python 3.8.2 64-bit' and replace it for another one. 


Monday, 21 September 2020

Python Anywhere issue with Disk Space Quota Limit.

The official help page for this on Python can be found at https://help.pythonanywhere.com/pages/DiskQuota/

Here's my notes on what I've tried so far. 

du -hs /tmp ~/.[!.]* ~/* /var/www/


4.0K /home/mysite/.bashrc4.0K /home/mysite/.gitconfig4.0K /home/mysite/.profile4.0K /home/mysite/.pythonstartup.py4.0K /home/mysite/.viminfo4.0K /home/mysite/README.txt4.0K /tmp8.0K /home/mysite/.local8.0K /home/mysite/.vimrc17M /home/mysite/.cache160M /home/mysite/aroundaboutslaundry321M /home/mysite/.virtualenvs


An easy fix is if the Cache is bloated. 

rm -rf ~/.cache

rm -rf ~/.cache/*

Or lets clean up Unused files. 

rm -rf /tmp/*
In my case this neither of these helped.    


CLEAN UP ANY UNUSED VIRTUALENVS

How see old virtual environments ( see what virtualenvs I have) 



lsvirtualenv

Running this command showed me that I only had the one Environment working . 




Uninstall python packages you don't need anymore

To see what packages you have take a look at your requirements.txt page or run 

pip list

I then ran through a handful that I believed I'm not using ; although I may need to install them again at a later date. 

To remove here is an example


pip uninstall django-cripsy-forms

And then Remove from requirements.txt.  



From here I could now install the package I wanted, which was 'wagtail' 

pip install wagtail







Thursday, 17 September 2020

ImportError: No module named django.core.management

 I got this error when running

python manage.py migrate


Traceback (most recent call last):

  File "manage.py", line 8, in <module>

    from django.core.management import execute_from_command_line

ImportError: No module named django.core.management


And then realised I should have run 

python3 manage.py migrate

Doh! 

Thursday, 20 August 2020

Adding Mobile Phone Check and UK Landline check together.

 In the current project I'm looking at I have two input fields for telephone numbers. 

1. UK Mobile Phone Number
2. UK Landline Number 

They each have their own regex checks .  


It's been requested that we should have just the one input field; this could be a UK landline OR UK Mobile .  So I'd like to merge the 2 regexes together.   Which is real easy but I found googling the exact answer very difficult.   So I thought I'd note it here. 

So here's the two seperate Regex's

Regex for UK Mobile Numbers. 

^(\+44\s?7(\d ?){3}|\(?07(\d ?){3}\)?)\s?(\d ?){3}\s?(\d ?){3}

Regex for UK Landlne Phone Numbers 

^\(?0( *\d\)?){9,10}$

So to use both we can add the OR statement which is '|' 

And to group the two sides together using the '(' and ')' 

which gives us the following combined statement. 

(^(\+44\s?7(\d ?){3}|\(?07(\d ?){3}\)?)\s?(\d ?){3}\s?(\d ?){3})|(^\(?0( *\d\)?){9,10}$)





Monday, 17 August 2020

Calling API from another Docker container returning a 'Cannot assign requested address' Error

This article is to document an issue I had recently that was caused by having two docker installation on my local computer and using them as API’s to each other. Doing this means you cannot call localhost like we would have previously. I found the issue quite hard to search for because at first I was unaware it was the Docker containers that were at fault. At first I had Drupal 7 website running locally and it would call my API ( Django ) , which was on another server.

However it made sense for development to have both things on my local environment; they would both be Docker installations. On doing so I'd set the Django API up on http://localhost:8000/ which I could see worked and could connect to using Postman. However when I tried to make the request through Drupal I would get the error 'Cannot assign requested address' Basically You cant use localhost for something within a container…

Each container thinks it is an entire server in its own right, and sees ‘localhost’ as only the container, and not the host. To fix this I did the following . On the Django ( API ) side open docker-compose.yml

Add "host.docker.internal" to the list of ALLOWED_HOSTS

for example.

1 - 'ALLOWED_HOST={"hosts": ["localhost", "api.localhost", "api.docker.localhost", "host.docker.internal"]}'

This was diagnosed by viewing the logs of the fafeapi container with docker-compose logs -f fafeapi  and seeing:
Also thank you to this post https://medium.com/it-dead-inside/docker-containers-and-localhost-cannot-assign-requested-address-6ac7bc0d042b