models py -coding utf8- from django contrib auth models import User fr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
models.py
#-*-coding:utf8-*-
from django.contrib.auth.models import User
from django.db import models
class ClientProfile(models.Model):
DAY_CHOICES = (
('Mo', u'Понедельник'),
('Tu', u'Вторник'),
('We', u'Среда'),
('Th', u'Четверг'),
('Fr', u'Пятница'),
('Sa', u'Суббота'),
('Su', u'Воскресенье'),
)
user = models.ForeignKey(User, unique=True)
urid_address = models.CharField(max_length=400, verbose_name=u"Юр.адрес:")
reg_address = models.CharField(max_length=400, verbose_name=u"Адрес регистрации:")
fact_address = models.CharField(max_length=400, verbose_name=u"Фактический адрес:")
time_work = models.TextField(max_length=400, verbose_name=u"Время работы:")
visit_day = models.CharField(max_length=2, verbose_name=u"День посещения:",
choices=DAY_CHOICES)
phone_number = models.IntegerField(max_length=16, verbose_name=u"Тел.номер:")
contact_face = models.CharField(max_length=64, verbose_name=u"Контактное лицо:")
is_trade = models.BooleanField(default=False)
forms.py
#-*-coding:utf8-*-
from django import forms
from django.contrib.auth.forms import UserCreationForm
from accounts.models import ClientProfile
from django.contrib.auth.models import User
class ClientForm(forms.ModelForm):
class Meta:
model = ClientProfile
exclude = ('user', 'is_trade')
class UserForm(UserCreationForm):
email = forms.EmailField()
def save(self, commit=True):
user = super(UserForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
views.py
#-*-coding:utf8-*-
from libs.decorators import render_to
from accounts.models import ClientProfile
from accounts.forms import ClientForm, UserForm
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
def reg_user(request):
"""This is view for registration user in database"""
if request.user.is_anonymous():
if request.method == 'POST':
form1 = UserForm(request.POST)
form2 = ClientForm(request.POST)
if form1.is_valid() and form2.is_valid():
user = form1.save()
form2.user = user
form2.save()
return render_to_response('accounts/welcome.html')
else:
return render_to_response('accounts/registration.html',
{'form1':form1, 'form2':form2})
form1 = UserForm()
form2 = ClientForm()
return render_to_response('accounts/registration.html',
{'form1':form1, 'form2':form2})
return render_to_response('accounts/login_success.html', {})