Advertisement
Guest User

Untitled

a guest
May 10th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #code for admin.py
  2.  
  3. from django.contrib.auth.models import User
  4. from django.contrib import admin
  5. from models import UserProfile
  6.  
  7. admin.site.unregister(User)
  8.  
  9. class UserProfileInline(admin.StackedInline):
  10. model = UserProfile
  11.  
  12. class UserProfileAdmin(admin.ModelAdmin):
  13. inlines = [ UserProfileInline, ]
  14. admin.site.register(User, UserProfileAdmin)
  15.  
  16. #code for form.py
  17.  
  18. from django import forms
  19. from django.forms import ModelForm
  20. from registration.forms import RegistrationForm
  21. from django.utils.translation import ugettext_lazy as _
  22. from models import UserProfile
  23. #from register.models import
  24. from django.contrib.auth.models import User
  25.  
  26. class UserRegistrationForm(RegistrationForm):
  27. GENDER_CHOICES = (
  28. ('M','Male'),
  29. ('F','Female'),
  30. )
  31. first_name = forms.CharField(label=_('First Name'), max_length=30, required=True)
  32. last_name = forms.CharField(label=_('Last Name'), max_length=30)
  33. gender = forms.CharField(label = _('Gender'), max_length=10)# choices = GENDER_CHOICES)
  34. dob = forms.DateField(
  35. ('%d/%m/%Y',),
  36. label=_('Date of Birth'),
  37. widget=forms.DateTimeInput(format = '%d/%m/%Y',attrs={ 'class':'input', 'readonly':'readonly', 'size':'15'}
  38. )
  39. )
  40. city = forms.CharField(max_length=100, label=_('City'))
  41. class Meta:
  42. models = UserProfile
  43.  
  44.  
  45. #code for model.py
  46.  
  47. from django.db import models
  48. from django.utils.translation import ugettext_lazy as _
  49. from django.contrib.auth.models import User, Group
  50.  
  51. class UserProfile(models.Model):
  52. GENDER_CHOICES=(
  53. ('M', 'Male'),
  54. ('F','Female'),
  55. )
  56.  
  57. user = models.OneToOneField(User)
  58. gender = models.CharField(_('Gender'), max_length=1, choices=GENDER_CHOICES, null=False)
  59. dob = models.DateField(_('Date of Birth'), null=False)
  60. city = models.CharField(_('City'), max_length=100, null=False)
  61.  
  62. #code for regbackend.py
  63.  
  64. from django.contrib.auth.models import User
  65. from forms import UserRegistrationForm
  66. from models import UserProfile
  67.  
  68. def user_created(sender, user, request, **kwargs):
  69. form = UserRegistrationForm(request.post)
  70. user_data = User(user=user)
  71. user_data.first_name = form.data['first_name']
  72. user_data.last_name = form.data['last_name']
  73. user_data.save()
  74.  
  75. data = UserProfile(user=user)
  76. data.gender = form.data['gender']
  77. data.dob = form.data['dob']
  78. data.user_type = form.data['user_type']
  79. data.city = form.data['city']
  80. data.save()
  81.  
  82. from registration.signals import user_registered
  83. user_registered.connect(user_created, sender = UserProfile)
  84.  
  85. #code for urls.py
  86.  
  87. from django.conf.urls import patterns, include, url
  88. from django.contrib import admin
  89. from forms import UserRegistrationForm
  90. #from registration.views import register
  91. #import registration.backends.default.urls as regUrls
  92. #import regbackend
  93.  
  94. urlpatterns = patterns('',
  95. url(r'^accounts/register/$', register, {'backend': 'registration.backends.default.DefaultBackend','form_class': UserRegistrationForm}, 'registration_register'),
  96. (r'^accounts/', include(regUrls)),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement