Get protocol + host name from URL

In my Django app, I need to get the host name from the referrer in request.META.get(‘HTTP_REFERER’) along with its protocol so that from URLs like: https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1 https://stackoverflow.com/questions/1234567/blah-blah-blah-blah http://www.example.com https://www.other-domain.com/whatever/blah/blah/?v1=0&v2=blah+blah … I should get: https://docs.google.com/ https://stackoverflow.com/ http://www.example.com https://www.other-domain.com/ I looked over other related questions and found about urlparse, but that didn’t do the trick since >>> … Read more

How to get Request.User in Django-Rest-Framework serializer?

I’ve tried something like this, it does not work. class PostSerializer(serializers.ModelSerializer): class Meta: model = Post def save(self): user = self.context[‘request.user’] title = self.validated_data[‘title’] article = self.validated_data[‘article’] I need a way of being able to access request.user from my Serializer class. 13 Answers 13

django change default runserver port

I would like to make the default port that manage.py runserver listens on specifiable in an extraneous config.ini. Is there an easier fix than parsing sys.argv inside manage.py and inserting the configured port? The goal is to run ./manage.py runserver without having to specify address and port every time but having it take the arguments … Read more

Reload django object from database

Is it possible to refresh the state of a django object from database? I mean behavior roughly equivalent to: new_self = self.__class__.objects.get(pk=self.pk) for each field of the record: setattr(self, field, getattr(new_self, field)) UPDATE: Found a reopen/wontfix war in the tracker: http://code.djangoproject.com/ticket/901. Still don’t understand why the maintainers don’t like this. 4 Answers 4