0

ユーザーを登録するためにフォームに入力するたびに、last_loginとdate_joinedが自動的に設定される必要があるため、プロセスが停止します。または、通常どおりサインアップできるようにどこに追加できますか?Views.py

import datetime



@csrf_exempt
def EmployerRegistration(request):
    print 'before'
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
            form = SignUpForm(request.POST)
            print form
            print"then"
            print form.errors
            if form.is_valid():
                print 'hello'
                employer =Employer.objects.create(username=form.cleaned_data["username"],password= form.cleaned_data['password'],first_name=form.cleaned_data['first_name'],last_name=form.cleaned_data['last_name'],email=form.cleaned_data['email'],address=form.cleaned_data['address'],mobile=form.cleaned_data['mobile'])
                employer.save()
                return HttpResponseRedirect('/login/')  



            else:
                print 'notvalid'
                return render_to_response('Sign_up_Employer.html',{'form':form}, context_instance=RequestContext(request))
            #return render_to_response('/lol/',{'form':form}, context_instance=RequestContext(request))
                last_login

    else:
        #user is not submitting show them the registeration form
                print 'not post'
                form= SignUpForm()
                context = {'form':form}
                return render_to_response('Sign_up_Employer.html',context,context_instance=RequestContext(request))

form.py

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.forms import ModelForm
from shoghlanah.models import *
import datetime


class SignUpForm(ModelForm):

    username      = forms.CharField(label=(u'Username'))
    first_name    = forms.CharField(label=(u'First name'))
    last_name     = forms.CharField(label=(u'Last name'))
    address       = forms.CharField(label=(u'Address'))
    email         = forms.EmailField(label=(u'Email Address'))
    password      = forms.CharField(label=(u'Password'),widget=forms.PasswordInput(render_value=False))
    password1     = forms.CharField(label=(u'Verify Password'),widget=forms.PasswordInput(render_value=False))
    mobile        = forms.IntegerField(label=(u'Mobile'))
    ## is it valid to put the here??
    last_login    =datetime.datetime.now()
    date_joined   =datetime.datetime.now()

    class Meta:
            model = Employer
        ## check whats Exclude !!!!
            exclude = ('user',)

    def clean_username(self):
            username = self.cleaned_data['username']
            try:
                User.objects.get(username=username)
                raise forms.ValidationError("That username already exists.Please select another")
            except User.DoesNotExist:
                return username
4

1 に答える 1

2

質問にいくつかの重要な情報 (雇用主のモデルなど) を追加していませんが、おそらく問題は、次の行で雇用主を保存するときです。

employer =Employer.objects.create(username=form.cleaned_data["username"],password= form.cleaned_data['password'],first_name=form.cleaned_data['first_name'],last_name=form.cleaned_data['last_name'],email=form.cleaned_data['email'],address=form.cleaned_data['address'],mobile=form.cleaned_data['mobile'])

次のプロパティがあります。

  • ユーザー名
  • パスワード
  • ファーストネーム
  • 苗字
  • Eメール
  • 住所
  • モバイル

問題が last_login と date_joined が必要な場合は、create()メソッド内に追加する必要があります:

import datetime
#last_login=datetime.datetime.now(), date_joined=datetime.date.now()

このようになるには:

employer =Employer.objects.create(last_login=datetime.datetime.now(),date_joined=datetime.datetime.now(),username=form.cleaned_data["username"],password= form.cleaned_data['password'],first_name=form.cleaned_data['first_name'],last_name=form.cleaned_data['last_name'],email=form.cleaned_data['email'],address=form.cleaned_data['address'],mobile=form.cleaned_data['mobile'])

それで問題を解決できます。


アップデート

を使用し.is_valid()ているので、以下のようにする必要があります。それ以外の:

if request.method == 'POST':
    form = SignUpForm(request.POST)
    print form
    print"then"
    print form.errors
    if form.is_valid():
        print 'hello'

必要なもの:

if request.method == 'POST':
    data = request.POST.copy()
    data['date_joined'] = datetime.date.today()
    data['last_login'] = datetime.datetime.now()

    form = SignUpForm(data)
    print form
    print"then"
    print form.errors
    if form.is_valid():
        print 'hello'
于 2012-04-20T20:01:17.550 に答える