# Random word on form

**URL:** <https://forum.djangoproject.com/t/random-word-on-form/10055>\
**Category:** Using Django\
**Created:** [October 10, 2021, 2:45pm UTC](https://forum.djangoproject.com/t/random-word-on-form/10055 "2021-10-10T14:45:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![typonaut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/typonaut/32/2316_2.png) [@typonaut](https://forum.djangoproject.com/u/typonaut)\
**Post date:** [October 10, 2021, 2:45pm UTC](https://forum.djangoproject.com/t/random-word-on-form/10055/1 "2021-10-10T14:45:29Z")

</div>

I am trying to place a random word on a form field’s placeholder, so that it is semi-dynamic. However, I am failing so far, because the random element seems to get locked in at server initialisation, rather than producing a random word each time.

The random word generator:

```auto
def random_search_prompt():
	thelist = [
				'barber',
				'take away',
				'hairdresser',
				'pub',
				'restaurant',
				'off licence',
				'architect',
			]
	return random.choice(thelist)

```

The form:

```auto
class DirectorySearchForm(forms.Form):
    q = forms.CharField(required=False, label=_('Search'), 
        widget=forms.TextInput(
            attrs={'placeholder': ('Search for ' + random_search_prompt())}
            )
        )

```

So, every time I get “barber”, until I restart Apache, and then I get another word (perhaps), until I restart again…

How to get a randomised placeholder on each load of the page?

Thanks

---

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guettli/32/1896_2.png) [@guettli](https://forum.djangoproject.com/u/guettli)\
**Post date:** [October 10, 2021, 5:36pm UTC](https://forum.djangoproject.com/t/random-word-on-form/10055/2 "2021-10-10T17:36:27Z")

</div>

When does random\_search\_prompt() get called?

I can tell you: It gets called during “import-time”, not at “request-time”.

You can set `attrs` during the constructor (` __init__ ()`) of your Form, then it will be called for every request.

---

<div class="post-metadata">

**Author:** ![typonaut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/typonaut/32/2316_2.png) [@typonaut](https://forum.djangoproject.com/u/typonaut)\
**Post date:** [October 10, 2021, 11:32pm UTC](https://forum.djangoproject.com/t/random-word-on-form/10055/3 "2021-10-10T23:32:13Z")

</div>

Thanks, looking at the `__init()__` almost got me to the answer, but once I had figured out how to update the `attrs` I started getting errors in the browser that the form had no attribute `get`, or `render_to_response`… which all started to seem very peculiar…

Then I remembered that this wasn’t a standard form view, and thus came to a solution like:

```auto
class DirectorySearchView(ListView):
	…
	def get_context_data(self, **kwargs):
		context = super(DirectorySearchView, self).get_context_data(**kwargs)
		context['form'] = DirectorySearchForm(self.request.GET or None)
		context['form'].fields['q'].widget.attrs.update({'placeholder': ("Search for… " + utilskit.random_search_prompt())})
		return context

```

The unfortunate part about this is that for each view on which this form resides (there are three) I have to effectively repeat this setup of the `context`. But I’ll figure that bit out later.
