0

How in Django i can access requset in form? I need this to get data tuple to pass in choices to form. Below init approach doesn't work: NameError: name 'request' is not defined, with self or without: self.request.GET.get('project') or request.GET.get('project')

class PostfilterForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super(PostfilterForm, self).__init__(*args, **kwargs)

    monitoring_words_to_show = Nlpmonitorword.objects.filter(monitoringwords__name = self.request.GET.get('project')) 
    words_list = []
    for word in monitoring_words_to_show:
        words_list.append((word.monitor_word, word.monitor_word))    
    words_list = tuple(words_list)   # trying to get here tuple to pass in choises (('vk', 'vk'), ('fb', 'fb'), ('vkfb', 'vkfb'))

    project = forms.CharField(required=True, label='')
    monitor = forms.MultipleChoiceField(widget=forms.SelectMultiple, choices=words_list, required=False, label='')
Vic Nicethemer
  • 1,081
  • 3
  • 16
  • 38
  • Possible duplicate of [How do I access the request object or any other variable in a form's clean() method?](http://stackoverflow.com/questions/1057252/how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clean-met) – Sayse Feb 29 '16 at 10:13
  • Duplicate refers to the actual question you're asking, but you have a bunch of code shown here that isn't in a method/function which is probably the real issue. – Sayse Feb 29 '16 at 10:15
  • @Sayse I saw that answer and pointed in question that it dont work for some reason – Vic Nicethemer Feb 29 '16 at 11:25

2 Answers2

1

What your form needs is not the request it's the project. It's better to deal with the request in the view and pass the required parameters to the form:

Form:

class PostfilterForm(forms.Form):
    def __init__(self, project, *args, **kwargs):
        self.project = project

View:

project = request.GET.get('project')
form = PostfilterForm(project, request.POST)
nima
  • 6,566
  • 4
  • 45
  • 57
  • this seems reasonable, could you point what goes after init string, i have tried def __init__(self, project, *args, **kwargs): self.project = kwargs.pop("project") super(PostfilterForm, self).__init__(project, *args, **kwargs) but got an error NameError: name 'project' is not defined – Vic Nicethemer Feb 29 '16 at 11:40
  • passing the parameter as a kwarg and then poping it is another way but since the form always needs the project and it's not an optional parameter I prefer to use it as a mandatory argument. Just remove the pop line and use the project because it is passed into the init function as a parameter by the view. – nima Feb 29 '16 at 11:47
  • hm, strange. with this edit: if pass 'project error NameError: name 'project' is not defined , then, if pass self.project the error NameError: name 'self' is not defined – Vic Nicethemer Feb 29 '16 at 11:56
  • Yes there was a typo in the view part I updated the answer. – nima Feb 29 '16 at 12:09
  • tried, the same errors. If i move code to init it works, projects is correctly passed to form, so i need to return words_list to form. The problem thai its returned the same way as project, ie i gor error words_list is no defined – Vic Nicethemer Feb 29 '16 at 12:38
  • Solved with http://stackoverflow.com/questions/5978884/django-forms-choicefield-automatically-generated-choices?rq=1 Your variant is working too, but putting project to first place in variables required me to put it also to initiate form, what in sequence results other errors with form rendering i cannot solve. thanx for help anyway – Vic Nicethemer Mar 01 '16 at 19:08
1

All the code you're trying to use isn't used within a method which means it doesn't belong to any instance of a PostFilterForm and therefore has no knowledge of self let alone its fields.

You should include these in a function, although what function that should be is unclear.

def my_function(self):
    monitoring_words_to_show = Nlpmonitorword.objects.filter(monitoringwords__name = self.request.GET.get('project')) 
    words_list = []
    for word in monitoring_words_to_show:
        words_list.append((word.monitor_word, word.monitor_word))    
    words_list = tuple(words_list)   # trying to get here tuple to pass in choises (('vk', 'vk'), ('fb', 'fb'), ('vkfb', 'vkfb'))
Sayse
  • 42,633
  • 14
  • 77
  • 146