如何在提交django自定义表单后保存用户的IP

时间:2017-07-04 07:05:05

标签: python django django-forms django-views ip

我编写了自己的表单来编辑个人资料,需要保存用户的ip,编辑个人资料,但不知道如何做到这一点。我知道,我们可以从request.META['REMORE_ADDR']得到那个ip,但是把它放在哪里以及如何保存到我的数据库......如果你能提供帮助,我们将非常高兴。

models.py

class Profile(models.Model):
   user = models.OneToOneField(User, unique=True)
   first_name = models.CharField(max_length=200)
   last_name = models.CharField(max_length=200)
   date_of_birth = models.DateField()
   biography = models.TextField()
   contacts = models.CharField(max_length=200)
   ip_address = models.GenericIPAddressField(null=True)

forms.py

class UserEditProfile(forms.ModelForm):
    first_name = forms.CharField( max_length=30)
    last_name = forms.CharField( max_length=30)
    date_of_birth = 
    forms.DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES))
    biography = forms.Textarea()
    contacts = forms.CharField()


    def __init__(self, *args, **kw):
        super(UserEditProfile, self).__init__(*args, **kw)
        self.fields['first_name'].initial = self.instance.first_name
        self.fields['last_name'].initial = self.instance.last_name
        self.fields['date_of_birth'].initial = 
        self.instance.date_of_birth
        self.fields['biography'].initial = self.instance.biography
        self.fields['contacts'].initial = self.instance.contacts


        self.fields.keyOrder = [
            'first_name',
            'last_name',
            'date_of_birth',
            'biography',
            'contacts'
            ]

    def save(self, *args, **kw):
        super(UserEditProfile, self).save(*args, **kw)
        self.instance.first_name = self.cleaned_data.get('first_name')
        self.instance.last_name = self.cleaned_data.get('last_name')
        self.instance.date_of_birth = 
        self.cleaned_data.get('date_of_birth')
        self.instance.biography = self.cleaned_data.get('biography')
        self.instance.contacts = self.cleaned_data.get('contacts')
        self.instance.save()

    class Meta:
        model = Profile
        fields = (
            'first_name',
            'last_name',
            'date_of_birth',
            'biography',
            'contacts'
        )
        exclude = ['user', 'ip_address']

view.py

def edit_profile(request):
    user = Profile.objects.get(id=request.user.id)

    if request.method == "POST":
        form = UserEditProfile(request.POST, instance=user)

        if form.is_valid():
            form.save(commit=False)
            return redirect('profile')
    else:
        form = UserEditProfile(instance=user)
        args = {'form': form}
        return render(request, 'edit.html', args)

1 个答案:

答案 0 :(得分:0)

您无法直接将request对象传递给表单。这不是它的工作方式。如果您需要将任何request属性与模型实例相关联,则应在视图中执行此操作。

您可以在视图中收集request.META['REMOTE_ADDR'] which gives the IP info of the logged in user in the view and associate it to your实例。

您可以在form.is_valid()方法

中执行此操作
if form.is_valid():
    profile = form.save(commit=False)
    profile.ip_address = request.META['REMOTE_ADDR']
    profile.user = request.user
    profile.save()
    return redirect('profile')