'Django Stripping Blanks In Between Words When Generating Blanks

I am working on a Hangman game using Django and I'm having some trouble generating the blanks from the words if the answer is two words, such as a first and last name. It seems to be something with Django stripping spaces and I have tried playing around with some solutions I found elsewhere, but unsuccessfully.

Here is one 'solution' I found in another custom fields.py file I created.

class ExampleAdmin(admin.ModelAdmin):
    ordering = ['display', ]

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        if db_field.name == 'display':
            kwargs['strip'] = False
        return super().formfield_for_dbfield(db_field, request, **kwargs)

And the other is a custom class within admin.py I created

class NonStrippingTextField(TextField):
    A TextField that does not strip whitespace at the beginning/end of
    it's value.  Might be important for markup/code.

    def formfield(self, **kwargs):
        kwargs['strip'] = False
        return super(NonStrippingTextField, self).formfield(**kwargs)

Here is the method of views.py where I am generating the blanks in the model to be displayed in the template.

if request.method == 'GET':
        load_animals_dict()
        word = generate_animals_word()
        game = Game(answer=word)

        for x in range(len(word)):
            if word[x] == ' ':
                game.display += ' '
            else:
                game.display += '_ '

        return render(request, 'index.html', {"game": game})

For testing purposes I have {{ game.display }} and {{ game.answer }} to see what renders and no matter what I have done - if there are two words the space in between always gets stripped.

EDIT: Splitting into two 'CharFields' I was able to resolve this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source