自定义Django注册和django配置文件

时间:2012-07-31 06:45:01

标签: python django django-forms django-registration

我正在努力与django用户注册1天我特此附上我的账户模块请考虑一下

这是我的模特

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from demo.models import Organization
def_max_length = 255



class Profile(models.Model):
    user_name = models.ForeignKey(User, unique=True,related_name = 'ussr')    
    first_name = models.CharField(max_length=100)
    middle_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    user_name = models.ForeignKey(User, unique=True, related_name='profile')
    password = models.CharField(max_length=80)
    role = models.CharField(max_length = 20)
    org_name = models.ForeignKey('demo.Organization',related_name = 'user_org_nm',null= False)


    city = models.CharField(max_length=50,null = True,blank = True)
    state = models.CharField(max_length=50,null = True,blank = True)
    country = models.CharField(max_length=50,null = True,blank = True)
    street = models.TextField(null=True,blank = True)
    pin = models.CharField(max_length=30,null = True,blank = True)


    user_type = models.CharField(max_length = 30)
    status =models.IntegerField(null=True, blank=True)
    primary_mobile =models.CharField(max_length = 50,null = True,blank = True)
    secondary_mobile =models.CharField(max_length = 50,null = True,blank = True)
    primary_landline =models.CharField(max_length = 50,null = True,blank = True)
    secondary_landline =models.CharField(max_length = 12,null = True,blank = True)
    primary_email = models.CharField(max_length = 30,null = True,blank = True)
    secondary_email = models.CharField(max_length = 30,null = True,blank = True)
    notes = models.TextField()
    date_created = models.DateTimeField(auto_now = True,blank=True, null=True)

    date_modified =models.DateTimeField(auto_now = False,null = True)
    class Meta:
        abstract=True



    """
    class UserProfile(Profile, User):
        def user_created(sender, user, request, **kwargs):
            form = ExtendedRegistrationForm(request.POST)
            extended_user = UserProfile(user=user)
            extended_user.is_active = False
            extended_user.first_name = form.extended_user['first_name']
            extended_user.last_name = form.extended_user['last_name']
            extended_user.pid = form.extended_user['pin']
            extended_user.street = form.extended_user['street']
            extended_user.number = form.extended_user['state']
            extended_user.number = form.extended_user['country']
            extended_user.number = form.extended_user['primary_mobile']
            extended_user.number = form.extended_user['secondary_mobile']
            extended_user.number = form.extended_user['primary_landline']
            extended_user.number = form.extended_user['secondary_landline']

            extended_user.number = form.extended_user['primary_email']
            extended_user.number = form.extended_user['secondary_email']

            extended_user.city = form.extended_user['city']
            extended_user.save()

        user_registered.connect(user_created)
    """


    def disable_from_user(self):
         try:
            get_mtng = Meeting.objects.filter(created_by = self.id)
            for get_p in get_mtng:
                get_p.status = 0
                get_p.save()
                if get_p:
                    try:
                        get_participant = Participant.objects.filter(meeting_id =get_p)
                        for get_pp in get_participant:
                            get_pp.status = 0
                            get_pp.save()
                    except:
                        LOG_INFO('Organization %s has no PARTICIPANT IN  '% get_p)
                        pass        
         except:
            pass 

    class Meta:
        app_label = 'accounts'

    def Validate(self):
        errors = {}

        errors.update(verifyStringEmpty('first_name', self.first_name, True))        
        errors.update(verifyStringEmpty('last_name', self.last_name, True))
        errors.update(verifyStringEmpty('passwd', self.passwd, True))
        errors.update(verifyStringEmpty('city', self.city, True))
        errors.update(verifyStringEmpty('state', self.state, True))
        errors.update(verifyStringEmpty('country', self.country, True))
        errors.update(verifyStringEmpty('street', self.street, True))
        errors.update(verifyStringEmpty('pin', self.pin, True))
        errors.update(verifyStringEmpty('primary_mobile', self.primary_mobile, True))
        errors.update(verifyStringEmpty('primary_landline', self.primary_landline, True))
        errors.update(get_email('primary_email', self.primary_email, True))
        errors.update(verifyStringEmpty('status', self.status, False))
        errors.update(verifyStringEmpty('middle_name', self.middle_name, False))
        errors.update(verifyStringEmpty('notes', self.notes, False))
        errors.update(verifyStringEmpty('secondary_mobile', self.secondary_mobile, False))
        errors.update(verifyStringEmpty('secondary_landline', self.secondary_landline, False))
        errors.update(get_email('secondary_email', self.secondary_email, False))

        return errors

    def ValidateAdmin(self):
        errors = {}

        errors.update(verifyStringEmpty('last_name', self.last_name, True))
        errors.update(verifyStringEmpty('passwd', self.passwd, True))
        #print self.get_org(self.org_name)
        errors.update(verifyStringEmpty('role', self.role, True))
        errors.update(verifyStringEmpty('address', self.address, True))
        errors.update(verifyStringEmpty('primary_mobile', self.primary_mobile, True))
        errors.update(verifyStringEmpty('primary_landline', self.primary_landline, True))
        errors.update(get_email('primary_email', self.primary_email, True))
        errors.update(verifyStringEmpty('status', self.status, False))
      #  errors.update(utils.verifyStringEmpty('org_type', self.org_type, False))
        errors.update(verifyStringEmpty('middle_name', self.middle_name, False))
        errors.update(verifyStringEmpty('notes', self.notes, False))
        errors.update(verifyStringEmpty('secondary_mobile', self.secondary_mobile, False))
        errors.update(verifyStringEmpty('secondary_landline', self.secondary_landline, False))
        errors.update(get_email('secondary_email', self.secondary_email, False))

        return errors



    class Meta:

        verbose_name = "User profile"
        verbose_name_plural = "User profiles"   

这是我的profile.py文件

from django import forms
from models import Profile
from demo.models import Organization
#import strings
from registration.forms import RegistrationForm


class UserRegistrationForm(RegistrationForm):
    first_name = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class' : 'myfieldclasscity'}),error_messages={'required':"please enter the First name"},required = True)
    middle_name = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class' : 'myfieldclasscity'}),error_messages={'required':"please enter the middle name"},required = True)
    last_name = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class' : 'myfieldclasscity'}),error_messages={'required':"please enter the last name"},required = True)

    password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
    password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput,
                                help_text = "Enter the same password as above, for verification.")
  #  primary_email = forms.EmailField(label="Email", max_length=75)
    secondary_email = forms.EmailField(label="secondary email", max_length=75)



    city = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class' : 'myfieldclasscity'}),error_messages={'required':"please enter the city name"},required = True)
    state = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class' : 'myfieldclassst'}),error_messages={'required':"please enter a state name"},required = True)
    country = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class' : 'myfieldclasscountry'}),error_messages={'required':"please enter a country name"},required = True)
    street = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class' : 'myfieldclassstreet'}),error_messages={'required':"please enter a street name"},required = True)
    pin = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class' : 'myfieldclasspin'}),error_messages={'required':"please enter a pin name"},required = True)
    primary_mobile =forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class' : 'myfieldclasspm'}),error_messages={'required':"work phone is required"},required = True)
    secondary_mobile =forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class' : 'myfieldclasssm'}),error_messages={'required':"home phone is required"},required = False)
    primary_landline =forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class' : 'myfieldclasspl'}),error_messages={'required':"work phone number  is required"},required = True)
    secondary_landline =forms.CharField(max_length=100,error_messages={'required':"home phone is required"},required = False)

    org_name = forms.ModelChoiceField(queryset=Organization.objects, label='Organization')

这是regbackend.py文件,我将保存所有自定义字段

import profile
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.contrib import messages

from demo.models import Organization
def user_created(sender, user, request, **kwargs):
    form = profile.UserRegistrationForm(request.POST)
    data = profile.Profile(user_name=user)        
    try:get_id = form.data['org_name']
    except:get_id = False
    get_org = Organization.objects.get(id = get_id)    
    data.org_name = get_org
    data.primary_email = user.email
    data.first_name = form.data['first_name']
    try:data.middle_name = form.data['middle_name']
    except:data.middle_name = ''
    data.last_name = form.data['last_name']
    data.city = form.data['city']
    data.street = form.data['street']
    data.state = form.data['state']
    data.country = form.data['country']
    data.primary_mobile = form.data['primary_mobile']
    try:data.secondary_mobile = form.data['secondary_mobile']
    except:data.secondary_mobile = ''
    data.primary_landline = form.data['primary_landline']
    try:data.secondary_landline = form.data['secondary_landline']
    except:data.secondary_landline  = ''
    try:data.secondary_email = form.data['secondary_email']
    except:data.secondary_email  = ''
    data.passworwd = user.password
    data.save()
    print "---------------------"
    print request
    messages.success(request, 'Thank you!')
 from registration.signals import user_registered
 user_registered.connect(user_created)

我的观点中的内容很少

from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.views import logout_then_login
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse, Http404
from accounts.forms import UserCreationForm
from django.contrib.auth.tokens import default_token_generator
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.conf import settings
from django.utils.http import urlquote, base36_to_int
from django.contrib.sites.models import Site 
from accounts.forms import UserCreationForm 
from django.core.urlresolvers import reverse


def logout_page(request):
    logout_then_login(request)



def index(request):
    return HttpResponseRedirect(reverse("registration_register"))

最后但并非最不重要的是urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

from django.contrib import admin
admin.autodiscover()
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

import registration.backends.default.urls as regUrls

    from profile import UserRegistrationForm
    from registration.views import register
    import regbackend, views
    from accounts import profile
    urlpatterns = patterns('',     
      #  (r'^conf/admin/(.*)', admin.site.root),
        url(r'^register/$', register, {'backend': 'registration.backends.default.DefaultBackend','form_class': UserRegistrationForm}, name='registration_register'),
        (r'^accounts/', include(regUrls)),
        url('^profile/$', direct_to_template, {'template': 'profile.html'}, name="profile"),
        (r'^$', views.index),    
    )

我可以保存自定义字段以及django默认字段,但

当我尝试在保存自定义字段(注册)后重定向url @ / accounts / profile /时,

问题开始。我用过

在regbackend.py中的data.save()之后的HttpresponseRedirect()方法,但它无法正常工作

请帮助我如何将网址重定向到用户个人资料。我对Signal不太了解。

2 个答案:

答案 0 :(得分:0)

你在使用django-registration吗?看起来你好吗?

答案 1 :(得分:0)

这来自文档(非常好):

Upon successful registration – not activation – the default redirect is to the URL pattern named registration_complete; this can be overridden by passing the keyword argument success_url to the register() view.

http://docs.b-list.org/django-registration/0.8/default-backend.html