將IP地址包含在表單is_valid中的最佳方式是什麼?讓我從一些代碼示例開始。嘗試驗證數據時,如何將多個字段添加到表單?
urls.py
from django.conf.urls.defaults import *
from testpost.views import TestPost
urlpatterns = patterns('',
(r'^djtestforms/', TestPost),
)
model.py
from django.db import models
class TestPostModel(models.Model):
name = models.CharField(max_length=100)
comment = models.CharField(max_length=100)
ip_address = models.IPAddressField()
def __unicode__(self):
return self.model
@models.permalink
def get_absolute_url(self):
return ('TestPostModel', [self.id])
forms.py
from django import forms
from models import TestPostModel
class TestPostForm(forms.ModelForm):
class Meta:
model = TestPostModel
from forms import TestPostForm
from models import TestPostModel
from django.http import HttpResponse
def TestPost(request):
f = TestPostForm(request.POST)
if f.is_valid():
object = f.save()
return HttpResponse("That worked")
else:
return HttpResponse("That didn't work")
我的問題是,當我嘗試做「F = TestPostForm(request.POST )「將IP地址添加到TestPostForm的最佳方式是什麼?我正在考慮沿着「f = TestPostForm(request.POST,ip_address = request.META [」REMOTE_ADDR「])」的行「但這不起作用。有什麼建議麼?
由於這個問題被問到的原因是爲了使表格有效,這段代碼根本沒有任何幫助。下面的答案是正確的。 – boatcoder 2010-08-05 22:03:09