add a registration
This commit is contained in:
parent
d8a1d9ef54
commit
1f3ab70dd6
|
@ -0,0 +1,24 @@
|
|||
from django import forms
|
||||
from webshop.models import Salutation, City
|
||||
|
||||
|
||||
class RegistrationForm(forms.Form):
|
||||
email = forms.EmailField()
|
||||
salutation = forms.ModelChoiceField(queryset=Salutation.objects.all())
|
||||
first_name = forms.CharField()
|
||||
last_name = forms.CharField()
|
||||
street_name = forms.CharField()
|
||||
street_number = forms.CharField()
|
||||
zip_code = forms.IntegerField(min_value=1000, max_value=9999)
|
||||
city = forms.CharField()
|
||||
|
||||
def clean_city(self):
|
||||
# Check that the two password entries match
|
||||
city = self.cleaned_data['city']
|
||||
zip_code = self.cleaned_data['zip_code']
|
||||
try:
|
||||
City.objects.get(name=city, zip_code=zip_code)
|
||||
except City.DoesNotExist:
|
||||
raise forms.ValidationError(
|
||||
"The zip code and the city don't match.")
|
||||
return city
|
|
@ -7,5 +7,6 @@
|
|||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit">Login</button>
|
||||
<p><a href="{% url 'registration' %}">Go to registration.</a></p>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
{% extends 'webshop/base.html' %}
|
||||
|
||||
{% block section_title %}Registration{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% if profile_form.errors %}
|
||||
<p style="color: red;">
|
||||
Please correct the error{{ form.errors|pluralize }} below.
|
||||
</p>
|
||||
{% endif %}
|
||||
<form action="" method="post" novalidate>
|
||||
<table>
|
||||
{{ user_form.as_table }}
|
||||
{{ profile_form.as_table }}
|
||||
</table>
|
||||
{% csrf_token %}
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -11,7 +11,6 @@ urlpatterns = [
|
|||
views.articles_in_category,
|
||||
name='category'),
|
||||
url('^', include('django.contrib.auth.urls')),
|
||||
url(r'^accounts/profile/$',
|
||||
views.profile,
|
||||
name='profile'),
|
||||
url(r'^profile/$', views.profile, name='profile'),
|
||||
url(r'^registration/$', views.registration, name='registration'),
|
||||
]
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from webshop.models import Article, Category, ArticleStatus, Person
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from webshop.models import Article, Category, ArticleStatus, Person, City
|
||||
from webshop.forms import RegistrationForm
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
@ -41,3 +45,32 @@ def profile(request):
|
|||
person = Person.objects.get(user=request.user)
|
||||
return render(request, 'registration/profile.html',
|
||||
{'person': person})
|
||||
|
||||
|
||||
def registration(request):
|
||||
if request.method == 'POST':
|
||||
profile_form = RegistrationForm(request.POST)
|
||||
user_form = UserCreationForm(request.POST)
|
||||
if (profile_form.is_valid() and user_form.is_valid()):
|
||||
pf = profile_form.cleaned_data
|
||||
uf = user_form.cleaned_data
|
||||
user = User.objects.create_user(uf['username'],
|
||||
pf['email'],
|
||||
uf['password2'])
|
||||
user.last_name = pf['last_name']
|
||||
user.first_name = pf['first_name']
|
||||
user.save()
|
||||
person = Person.objects.create(
|
||||
salutation=pf['salutation'],
|
||||
city=City.objects.get(zip_code=pf['zip_code'],
|
||||
name=pf['city']),
|
||||
street_name=pf['street_name'],
|
||||
street_number=pf['street_number'],
|
||||
user=user)
|
||||
return HttpResponseRedirect('/login/')
|
||||
else:
|
||||
profile_form = RegistrationForm
|
||||
user_form = UserCreationForm
|
||||
return render(request, 'registration/register.html',
|
||||
{'profile_form': profile_form,
|
||||
'user_form': user_form})
|
||||
|
|
Loading…
Reference in New Issue