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.