python - How to access the request object in FormView -
i want access request object in formview , client ip. know, it's not reliable put hidden-field in html form because can changed user if user wants so. therefore, choose in backed. use modelform form_class
of formview , exclude ip
field in form. here come problem, know how add ip
field dynamically. can't save model successfully. here code.
models.py
class mymodel(models): ip = models.genericipaddressfield() [...]
forms.py
class myform(forms.modelform): class meta: model = mymodel exclude = ['ip']
views.py
class myview(formview): form_class = myform [...]
i've tried change psot()
method , manually added ip
field of request.post
found didn't know how pass form instance..i spent night solve problem , failed .. tanks helping in advance..
thanks daniel answering question.
i changed code this:
views.py
class pollview(formview): template_name = 'poll.html' form_class = pollform success_url = '/thanks/' def get_ip(self,request): if request.meta.has_key('http_x_forwarded_for'): ip = request.meta['http_x_forwarded_for'] else: ip = request.meta['remote_addr'] return ip def form_valid(self, form): form.instance.ip = self.get_ip(self.request) super(pollview,self).form_valid(form)
but got error:
valueerror @ / view poll.views.pollview didn't return httpresponse object. returned none instead.
Comments
Post a Comment