[Answered ]-Django 'str' object has no attribute 'as_widget'

2๐Ÿ‘

โœ…

If your form is not valid, you re-render the template but you do not add the form to the context. So, when the template comes to render {{ form.username }} or whatever, it does not find it โ€“ and the default when a variable is not found is to use an empty string. So, it is the empty string that gets passed to the filter, hence the error you see.

The solution is to ensure you always pass the form into the template:

else:
    form = UserForm()
return render(request, 'register_user.html', {'form': form})
๐Ÿ‘คDaniel Roseman

Leave a comment