每当我填写表单注册用户时,进程停止,因为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
答案 0 :(得分: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'])
您拥有以下属性:
如果您的问题是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'