Guest User

Untitled

a guest
Nov 27th, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # forms.py
  2. class ProfileForm(forms.ModelForm):
  3.  
  4.     city = forms.ModelChoiceField(queryset=City.objects.all())
  5.  
  6.     class Meta:
  7.         model = Profile
  8.         fields = ['country', 'region']
  9.  
  10. # views.py
  11. def signup(request):
  12.     message = ''
  13.  
  14.     if request.method == 'POST':
  15.         pform = ProfileForm(request.POST)
  16.         pform.is_valid()
  17.         return HttpResponse('%s' % pform)
  18.     else:
  19.         try:
  20.             ip = request.META.get('REMOTE_ADDR', '') or request.META.get(
  21.                 'HTTP_X_FORWARDED_FOR', '')
  22.             info = get_ip_info(ip)
  23.             region = Region.objects.filter(name__contains=info['region'])[0]
  24.             city = City.objects.filter(name__contains=info['city'])[0]
  25.  
  26.         pform = ProfileForm(initial={
  27.             'region': region.id,
  28.             'city': city.id,
  29.             'country': 1
  30.         })
  31.         pform.fields['city'].queryset = region.city_set.all()
  32.  
  33.     return render(request, 'authenticate/signup.html', {
  34.         'pform': pform,
  35.         'message': message,
  36.     })
Add Comment
Please, Sign In to add comment