Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.db import models
- # Create your models here.
- class User(models.Model):
- first_name = models.CharField(max_length = 50)
- last_name = models.CharField(max_length = 100)
- username = models.CharField(max_length=30)
- email = models.EmailField(unique = True)
- password1 = models.CharField(max_length=255)
- password2 = models.CharField(max_length=255)
- import re
- from django import forms
- from django.contrib.auth.models import User
- from django.contrib.auth.forms import UserCreationForm
- from django.core.exceptions import ObjectDoesNotExist
- class RegistrationForm(UserCreationForm):
- email = forms.EmailField(required = True)
- class Meta:
- model = User
- fields = ('username',
- 'first_name',
- 'last_name',
- 'email',
- 'password1',
- 'password2' )
- help_texts = {
- 'username': None,
- 'email': None,
- 'password': None,
- }
- def clean(self):
- cleaned_data = super(RegistrationForm, self).clean()
- password1 = self.cleaned_data.get("password1")
- password2 = self.cleaned_data.get("password2")
- if not password2:
- raise forms.ValidationError("You must confirm your password")
- if password1 != password2:
- self.add_error('password2', "Password does not match")
- return cleaned_data
- def save(self, commit=True):
- user = super(RegistrationForm, self).save(commit = False)
- user.first_name = self.cleaned_data['first_name']
- user.last_name = self.cleaned_data['last_name']
- user.email = self.cleaned_data['email']
- password = self.cleaned_data.get("password2")
- if commit:
- user.save
- return user
- {% extends 'base.html'%}
- {% load staticfiles %}
- {% block content%}
- {% load bootstrap %}
- <div class = "signup-box">
- <h2>Sign up</h2>
- <form class="form-horizontal" action="signup" method="post">
- {% csrf_token %}
- <!-- {{ form|bootstrap }} -->
- {{ form.username.label }}
- {{ form.username }}
- {{ form.first_name.label }}
- {{ form.first_name }}
- {{ form.last_name.label }}
- {{ form.last_name }}
- {{ form.email.label }}
- {{ form.email }}
- {{ form.password1.label }}
- {{ form.password1 }}
- {{ form.password2.label }}
- {{ form.password2 }}<br>
- <input type="submit" value="Submit" />
- </form>
- </div>
- {% endblock %}
- def signup(request):
- '''This function is meant to collect data from the signup page
- and post the information to the user table in the database.
- I currently cannot post the information to the database'''
- if request.method == "POST":
- form = RegistrationForm(request.POST)
- if form.is_valid():
- form.save()
- return HttpResponse('<h1>The form is valid!<h1>')
- return redirect('/login.html')
- else:
- form = RegistrationForm(request.POST)
- messages.error(request, "Error")
- return render(request, 'signup', args)
- else:
- form = RegistrationForm()
- return render(request, 'signup.html', {'form': form})
- url(r'^$', views.home),
- url(r'^login/$', views.login, name='login'),
- url(r'^signup/$', views.signup, name='signup'),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement