Reputation: 2135
I am using django-countries to get a flag from the CountryField and it works. However, some users don't have a country set on their profiles, but I still have to display a flag. My idea was to get a country from IP and then get a flag from the country or anything similar to that.
Unfortunately, this is not happening in the view, so I don't have the request. I can set the IP in the template and then, in the backend, check if no flag was found (if a country is not provided) and then somehow use the IP that I got from the template to get a flag. Is something like that possible? If needed, GeoDjango is already included in the project.
Perhaps there is a totally different way of doing this, but I am new to Django and still learning. Thanks.
Upvotes: 0
Views: 1974
Reputation: 20563
Personally I would set a generic flag as a fallback (django flag perhaps?) if user does not set their country, and to be fair there must be a reason if user doesn't. Or you can simply make that country field mandatory.
And assuming that person in that country based on their existing IP just doesn't sound right. Imagine if you are working remotely via VPN or behind a proxy server... you get the idea.
It makes more sense if you just want to redirect people to sub-domain (different language) site base on IP.
Upvotes: 0
Reputation: 747
First, you should look up the country in the view, not the template. The view is supposed to do all DB work and business logic, the template simply display in the best way possible what the view fetches. If you want to apply it to, say, every page, then consider using a context processor.
Regarding getting the country from the IP, this question explains it: Django, Retrieve IP location
Upvotes: 0