【发布时间】:2025-12-03 23:10:01
【问题描述】:
我正在尝试使用django-ipware pacakge 记录通过 Django 提交表单的某人的 IP 地址。
表单是一个模型表单。这是模型:
# models.py
from django.db import models
class Upload(models.Model):
email = models.EmailField()
title = models.CharField(max_length=100)
language = models.ForeignKey('about.channel', on_delete=models.CASCADE, default='Other')
date = models.DateField(auto_now_add=True)
file = models.FileField()
ip_address = models.GenericIPAddressField()
这是表格:
# forms.py
class UploadForm(forms.ModelForm):
class Meta:
model = Upload
fields = ['email', 'title', 'language', 'file']
labels = {
'email': 'E-Mail',
'title': 'Video Title',
'file': 'Attach File'
}
获取 IP 地址的业务逻辑非常简单,但我尝试将其放置在不同的位置,但没有成功。我应该将逻辑放在哪里,以便与其他表单数据一起提交?
# views.py
from ipware.ip import get_real_ip
ip = get_real_ip(request)
【问题讨论】:
标签: python django python-3.x django-forms