Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         ip = request.META.get('REMOTE_ADDR', '') or request.META.get(
  20.             'HTTP_X_FORWARDED_FOR', '')
  21.         info = get_ip_info(ip)
  22.         region = Region.objects.filter(name__contains=info['region'])[0]
  23.         city = City.objects.filter(name__contains=info['city'])[0]
  24.  
  25.         pform = ProfileForm(initial={
  26.             'region': region.id,
  27.             'city': city.id,
  28.             'country': 1
  29.         })
  30.         pform.fields['city'].queryset = region.city_set.all()
  31.  
  32.     return render(request, 'authenticate/signup.html', {
  33.         'pform': pform,
  34.         'message': message,
  35.     })
  36.  
  37. # models.py тот же
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement