AttributeError: 'str' object has no attribute 'fields' Using Django non rel on GAE
In your login.html
template, you have {{ form|as_bootstrap }}
, but in your code for signing up, you are rendering the template for login, but you are not passing in the form value:
return render_to_response('presentacion/login.html',
context_instance=RequestContext(request))
There is no context here.
You need to fix this by redirecting the user to the login view, instead of rendering the login template from your sign up view.
In addition, you should use the render
shortcut which will automatically send RequestContext
.
Your are also not checking for duplicate users.
To fix these issues in your code:
from django.shortcuts import render, redirect
def signup_view(request):
form = RegisterForm(request.POST or None)
ctx = {'form': form}
if request.method == "POST":
if form.is_valid():
username = form.cleaned_data['username']
name = form.cleaned_data['name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
password_one = form.cleaned_data['password_one']
password_two = form.cleaned_data['password_two']
if not User.objects.filter(email=email).exists():
newUser = User.objects.create_user(username=username,
first_name=name,
last_name=last_name,
email=email,
password=password_one)
newUser.save()
else:
# Do something, because a user
# with this email already exists
pass
return redirect('login')
return render(request, 'presentacion/sign_up.html', ctx)
If you ended up here like me with a similar error, in Django 2.2.4, using as_table|crispy
doesn't seem to work at all. The solution was to remove as_table
:
Before
<div class="col-md-4 ">
<form action="." method="POST">
{%csrf_token%}
{{ form.as_table|as_bootstrap}}
<button type="submit" class="btn btn-default">Sign UP</button>
</form>
</div>
After
<div class="col-md-4 ">
<form action="." method="POST">
{%csrf_token%}
{{ form|as_bootstrap}}
<button type="submit" class="btn btn-default">Sign UP</button>
</form>
</div>
This may not answer the OP's question, but if you ended up here like me, hope this helps you.