Django – Simple custom template tag example

Here is my solution (based on a custom tag): Firstly create the file structure. Go into the app directory where the tag is needed, and add these files: templatetags templatetags/__init__.py templatetags/video_tags.py The templatetags/video_tags.py file: from django import template register = template.Library() @register.simple_tag def get_rate(crit, rates): return rates.get(crit=crit).rate The template part, with our tag call: {% … Read more

How to install libpq-fe.h?

For some reason, the file is missing on the system. As you’re using apt-get, the system is dpkg based, presumably Debian or it’s derivative. You can try the Ubuntu’s package search to get which package contains a file with name ending in libpq-fe.h. I found the package is libpq-dev and file’s absolute path is /usr/include/postgresql/libpq-fe.h. … Read more

Django forms request.user

I think you can achieve this by overriding the __init__() method of the form, passing in an instance of User and filtering the queryset using that user. Something like this: class TrophiesForm(ModelForm): used_his = forms.ModelMultipleChoiceField(queryset=Gun.objects.filter(user__id=1)) def __init__(self, user, *args, **kwargs): super(TrophiesForm, self).__init__(*args, **kwargs) self.fields[‘used_his’].queryset = User.objects.filter(pk = user.id) In your view you can pass in … Read more