Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. from django.db import models
  2.  
  3. # Create your models here.
  4. class User(models.Model):
  5. first_name = models.CharField(max_length = 50)
  6. last_name = models.CharField(max_length = 100)
  7. username = models.CharField(max_length=30)
  8. email = models.EmailField(unique = True)
  9. password1 = models.CharField(max_length=255)
  10. password2 = models.CharField(max_length=255)
  11.  
  12. import re
  13. from django import forms
  14. from django.contrib.auth.models import User
  15. from django.contrib.auth.forms import UserCreationForm
  16. from django.core.exceptions import ObjectDoesNotExist
  17.  
  18.  
  19. class RegistrationForm(UserCreationForm):
  20. email = forms.EmailField(required = True)
  21. class Meta:
  22. model = User
  23. fields = ('username',
  24. 'first_name',
  25. 'last_name',
  26. 'email',
  27. 'password1',
  28. 'password2' )
  29. help_texts = {
  30. 'username': None,
  31. 'email': None,
  32. 'password': None,
  33. }
  34.  
  35. def clean(self):
  36. cleaned_data = super(RegistrationForm, self).clean()
  37. password1 = self.cleaned_data.get("password1")
  38. password2 = self.cleaned_data.get("password2")
  39.  
  40. if not password2:
  41. raise forms.ValidationError("You must confirm your password")
  42. if password1 != password2:
  43. self.add_error('password2', "Password does not match")
  44. return cleaned_data
  45.  
  46. def save(self, commit=True):
  47. user = super(RegistrationForm, self).save(commit = False)
  48. user.first_name = self.cleaned_data['first_name']
  49. user.last_name = self.cleaned_data['last_name']
  50. user.email = self.cleaned_data['email']
  51. password = self.cleaned_data.get("password2")
  52.  
  53. if commit:
  54. user.save
  55. return user
  56.  
  57. {% extends 'base.html'%}
  58. {% load staticfiles %}
  59. {% block content%}
  60. {% load bootstrap %}
  61.  
  62. <div class = "signup-box">
  63. <h2>Sign up</h2>
  64. <form class="form-horizontal" action="signup" method="post">
  65. {% csrf_token %}
  66. <!-- {{ form|bootstrap }} -->
  67.  
  68. {{ form.username.label }}
  69. {{ form.username }}
  70. {{ form.first_name.label }}
  71. {{ form.first_name }}
  72. {{ form.last_name.label }}
  73. {{ form.last_name }}
  74. {{ form.email.label }}
  75. {{ form.email }}
  76. {{ form.password1.label }}
  77. {{ form.password1 }}
  78. {{ form.password2.label }}
  79. {{ form.password2 }}<br>
  80. <input type="submit" value="Submit" />
  81. </form>
  82. </div>
  83.  
  84. {% endblock %}
  85.  
  86. def signup(request):
  87. '''This function is meant to collect data from the signup page
  88. and post the information to the user table in the database.
  89. I currently cannot post the information to the database'''
  90. if request.method == "POST":
  91. form = RegistrationForm(request.POST)
  92. if form.is_valid():
  93. form.save()
  94. return HttpResponse('<h1>The form is valid!<h1>')
  95. return redirect('/login.html')
  96. else:
  97. form = RegistrationForm(request.POST)
  98. messages.error(request, "Error")
  99. return render(request, 'signup', args)
  100. else:
  101. form = RegistrationForm()
  102. return render(request, 'signup.html', {'form': form})
  103.  
  104. url(r'^$', views.home),
  105. url(r'^login/$', views.login, name='login'),
  106. url(r'^signup/$', views.signup, name='signup'),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement