Reputation: 101
I want to create an account registration page that links directly to an account info page and displays the account info. I'm having a hard time getting the form to save the account info into my model and I don't know why.
models.py:
class Owner(models.Model):
fullname = models.CharField(max_length=255)
username = models.CharField(max_length=255)
password = models.CharField(max_length=255)
email = models.EmailField()
def __unicode__(self):
return self.fullname
""" This is a Form class and not a regular Model class """
class OwnerForm(forms.Form):
class Meta:
model = Owner
fields = ['fullname', 'username', 'password', 'email']
views.py:
def register(request):
form = OwnerForm()
if request.POST:
form = OwnerForm(request.POST)
if form.is_valid():
fullname = request.POST.get('fullname', '')
username = request.POST.get('username', '')
password = request.POST.get('password', '')
email = request.POST.get('email', '')
owner_obj = Owner(fullame=fullname, username=username, password=password, email=email)
owner_obj.save()
return HttpResponseRedirect('courses/accountinfo.html')
else:
form = OwnerForm()
return render_to_response('courses/register.html', {'form': form}, context_instance=RequestContext(request))
register.html:
{% extends "layout.html" %}
{% load static from staticfiles %}
{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<article>
<div id="Register">
<form action="{% url 'courses:accountinfo' %}" method="post"> {% csrf_token %}
<p>
<label for="fullname">Full name:</label>
<input id="fullname" name="fullname" type="text">
</p>
<p>
<label for="email">Email</label>
<input id="email" name="email" type="text">
</p>
<p>
<label for="username">Username</label>
<input id="username" name="username" type="text">
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password">
<span>Enter a password longer than 8 characters</span>
</p>
<p>
<label for="confirm_password">Confirm Password</label>
<input id="confirm_password" name="confirm_password" type="password">
<span>Please confirm your password</span>
</p>
<p>
<input type="submit" value="REGISTER" id="submit">
</p>
</form>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="{% static 'js/app.js' %}" charset="utf-8"></script>
</div>
</article>
{% endblock %}
Upvotes: 0
Views: 77
Reputation: 490
Use the save method on the form to save the data rather than creating a model object once the form is valid. However I see that the template contains additional fields. Do you have something in the valid method? What is the error that you are getting? are you posting to the correct url?
Upvotes: 0
Reputation: 137
You can call this after checking if the form.is_valid
, then send the redirect and let your other view handle the rest. The issue may also be with the accountinfo
view, but you didn't post that code.
owner = form.save()
owner.set_password(owner.password)
owner.save()
Upvotes: 1