2012-03-06 3 views
1

오케이 다시 묻습니다.장고 등록 및 데이터 저장

이번엔 백엔드에서 장고 등록을 사용하고 있습니다.

내 regbackend.py가 다른 많은 기능을 갖고있는 이유에 대해 궁금한 사람이 있으면 서버가 해당 메소드에 대해 불만을 제기하고 있으므로 방금 복사하여 붙여 넣었습니다.

그러나 백엔드가 데이터베이스에 저장할 저장 함수를 어떻게 호출하는지 알고 싶습니다.

저장 방법은 여기에 포함되어 있기 때문에 여기에 포함 시켰습니다. 그렇지 않으면 이미 내 forms.py에 있습니다.

제발 도와주세요, 사실 pinax 및 django 프로필을 보았습니다.하지만 내 프로젝트를 완전히 개편해야합니다.

정말 고마워요.

forms.py :

from django import forms 
from r2.models import Keyword 
from r2.models import UserProfile 
from registration.forms import RegistrationForm 
from registration.models import RegistrationProfile 
from django.utils.translation import ugettext_lazy as _ 
from registration.forms import RegistrationForm, attrs_dict 

class ProjectSpecificRegistrationForm(RegistrationForm): 
    keywords = forms.ModelChoiceField(queryset=Keyword.objects.all()) 
    first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name')) 
    last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name')) 

    def save(self, profile_callback=None): 
     new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'], 
     password=self.cleaned_data['password1'], 
     email=self.cleaned_data['email']) 
    new_profile = UserProfile(user=new_user,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name']) 

     new_profile.save() 
     return new_user() 

Urls.py는 :

from django.conf import settings 
from django.contrib.sites.models import RequestSite 
from django.contrib.sites.models import Site 
from django.contrib.auth.models import User 
from registration import signals 
from registration.forms import RegistrationForm 
from registration.models import RegistrationProfile 
from r2.forms import ProjectSpecificRegistrationForm 
from r2.models import * 

class RegBackend(object): 

    def register(self, request, **kwargs): 

     username, email, password = kwargs['username'], kwargs['email'], kwargs['password1'] 
     if Site._meta.installed: 
      site = Site.objects.get_current() 
     else: 
      site = RequestSite(request) 
     new_user = RegistrationProfile.objects.create_inactive_user(username, email, 
                    password, site) 
    user = User.objects.get(username=new_user.username) 
     user.first_name = kwargs['first_name'] 
     user.last_name = kwargs['last_name'] 
    user.keywords = kwargs['keywords'] 

     signals.user_registered.send(sender=self.__class__, user=new_user, request=request) 
    user.save() 
     return new_user 

    def save(self, profile_callback=None): 
     print('Came in') 
     new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'], 
     password=self.cleaned_data['password1'], 
     email=self.cleaned_data['email']) 
    new_profile = UserProfile(user=new_user,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name']) 

     new_profile.save() 
     return new_user() 

    def registration_allowed(self, request): 
     return getattr(settings, 'REGISTRATION_OPEN', True) 

    def post_registration_redirect(self, request, user): 
     return ('registration_complete',(), {}) 
+0

의도가 잘못되었습니다. – DrTyrsa

답변

1

CR하려면 : 내 regbackend.py 여기

url(r'^accounts/register/$',register, {'backend': 'registration.regbackend.RegBackend','form_class' : ProjectSpecificRegistrationForm}, name='registration_register'), 
url(r'^accounts/', include('registration.backends.default.urls')), 

입니다 새 사용자를 초대하려면 register 백엔드 방법을 사용해야합니다. 서버가 그래서 난 그냥 복사가 붙여 그 방법에 대해 불평

backend = RegBackend() 
user = backend.register(request, **your_kwargs) 

당신은 거기 request에 합격해야합니다, 그래서는 (request에 액세스 할 수 있습니다 또는 다른 장소) 뷰에서 호출해야합니다.

단지 (registration.backends.default.DefaultBackend에서) 상속을 사용

그리고 등록 백엔드하지 않는 (그리고 안) save 방법이 아무것도 복사 할 필요가 없습니다.

+0

아, 맞아. 좋아, 내가 그것을 밖으로 시도하자! DrTyrsa에게 감사드립니다! –

+0

실제로 그것은 이미 내 백엔드의 등록 메소드를 사용하고 있습니다. 몇 줄을 인쇄했고 콘솔이 출력합니다. 내가 할 수없는 일은 내 데이터베이스에 저장하는 것입니다. @DrTyrsa –

+0

@StanwinSiow 귀하의 의도를 확인하십시오. 그리고'keywords' 메소드를'keyword'를 추가하고 싶다면'save' 메소드가 호출되지 않습니다. – DrTyrsa

관련 문제