View difference between Paste ID: DtRetM15 and gvLSEHGM
SHOW: | | - or go back to the newest paste.
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:
19+
        ip = request.META.get('REMOTE_ADDR', '') or request.META.get(
20-
            ip = request.META.get('REMOTE_ADDR', '') or request.META.get(
20+
            'HTTP_X_FORWARDED_FOR', '')
21-
                'HTTP_X_FORWARDED_FOR', '')
21+
        info = get_ip_info(ip)
22-
            info = get_ip_info(ip)
22+
        region = Region.objects.filter(name__contains=info['region'])[0]
23-
            region = Region.objects.filter(name__contains=info['region'])[0]
23+
        city = City.objects.filter(name__contains=info['city'])[0]
24-
            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-
    })
36+
37
# models.py тот же