ユーザーがDjango-Registerに登録された後、auth.Userから拡張されたユーザープロファイルを作成するフォームを送信する際に問題が発生しました。
エラーが表示されます:Please correct the following fields:
テンプレート内のコードの文字列から:
{% if form.errors %}<p>Please correct the following fields:</p>{% endif %}
フォームにエラーがあるかどうかがわかります。残りのフォーム テンプレートは次のようになります。
<div class='register_div'>
{% if form.first_name.errors %}
<p class='error'>{{ form.first_name.errors }}</p>
{% endif %}
<p>
<label for='first_name'{% if form.first_name.errors %}class='error'{% endif %}>
First Name:
</label>
</p>
<p>{{ form.first_name }}</p>
</div>
私のモデル.py
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
role = models.CharField(max_length=14, choices=role, verbose_name='Role', blank=True, null=True)
job_description = models.TextField(max_length=100, verbose_name='Job Description', blank=True)
# Basic Information
first_name = models.CharField(max_length=30, verbose_name='First Name')
last_name = models.CharField(max_length=30, verbose_name='Last Name')
dob = models.DateField(verbose_name='Date of Birth')
stud_id = models.BigIntegerField(verbose_name='Student ID',)
mob_num = models.CharField(max_length=30, verbose_name='Mobile Number')
home_num = models.CharField(max_length=30, verbose_name='Home Number')
term_add = models.TextField(verbose_name='Term Time Address')
perm_add = models.TextField(verbose_name='Permanent Address')
au_status = models.NullBooleanField(verbose_name='Are you a member of the AU?')
# Next of Kin Details
nok_first_name = models.CharField(max_length=30, verbose_name='First Name (Next of Kin) ')
nok_last_name = models.CharField(max_length=30, verbose_name='Last Name (Next of Kin)')
relationship = models.CharField(max_length=30, verbose_name='Relationship (with Next of Kin)')
nok_mob_num = models.CharField(max_length=30, verbose_name='Mobile Number (Next of Kin)')
nok_home_num = models.CharField(max_length=30, verbose_name='Home Number (Next of Kin)')
nok_add = models.TextField(verbose_name='Address (Next of Kin)')
# Health and Safety
diet = models.TextField(verbose_name='Dietary Requirements', blank=True)
med_cond = models.TextField(verbose_name='Medical Conditions', blank=True)
fear = models.TextField(verbose_name='Greatest Fears', blank=True)
swim = models.NullBooleanField(verbose_name='Can you swim more than 100m?')
# Equipment:
sleeping_bag = models.NullBooleanField(verbose_name='Do you have a warm sleeping bag')
waterproof = models.NullBooleanField(verbose_name='Do you have waterproof clothing?')
shoes = models.NullBooleanField(verbose_name='Do you have a pair of climbing shoes?')
shoe_size = models.DecimalField(verbose_name='What is your athletic shoe size (in UK sizes)?', max_digits=3, decimal_places=1, blank=True, null=True)
harness = models.NullBooleanField(verbose_name='Do you have a harness?')
belay_device = models.NullBooleanField(verbose_name='Do you have a belay plate and a screwgate?')
helmet = models.NullBooleanField(verbose_name='Do you have a helmet?')
nut = models.NullBooleanField(verbose_name='Do you have a set of nuts?')
hex = models.NullBooleanField(verbose_name='Do you have a set of hexes?')
# Misc.:
minibus = models.NullBooleanField(verbose_name='Can you drive the SU minibus?')
私のforms.py
class RegistrationForm(ModelForm):
# Basic Information
first_name = forms.RegexField(label='First Name', regex=r'^[A-Z][a-zA-Z\s]+$',
error_messages = {'invalid': "A name starts with a capital letter,
and may contain only alphabetical characters."})
last_name = forms.RegexField(label='Last Name', regex=r'^[A-Z][a-zA-Z\s]+$',
error_messages = {'invalid': "A name starts with a capital letter,
and may contain only alphabetical characters."} )
# Details
au_status = forms.TypedChoiceField(label='Are you a member of the AU?', required=False, choices=choices, coerce=int)
# Next of Kin Details
nok_first_name = forms.RegexField(label='First Name (Next of Kin)', regex=r'^[A-Z][a-zA-Z\s]+$',
error_messages = {'invalid': "A name starts with a capital letter,
and may contain only alphabetical characters."})
nok_last_name = forms.RegexField(label='Last Name (Next of Kin)',
regex=r'^[A-Z][a-zA-Z\s]+$',
error_messages = {'invalid': "A name starts with a capital
letter, and may contain only alphabetical characters."})
# Health and Safety
swim = forms.TypedChoiceField(label='Can you swim more than 100m?',
required=False, choices=choices, coerce=int)
# Equipment:
sleeping_bag = forms.TypedChoiceField(label='Do you have a warm sleeping bag', required=False, choices=choices, coerce=int)
waterproof = forms.TypedChoiceField(label='Do you have waterproof clothing?', required=False, choices=choices, coerce=int)
shoes = forms.TypedChoiceField(label='Do you have a pair of climbing shoes?', required=False, choices=choices, coerce=int)
harness = forms.TypedChoiceField(label='Do you have a harness?', required=False, choices=choices, coerce=int)
belay_device = forms.TypedChoiceField(label='Do you have a belay plate and a screwgate?', required=False, choices=choices, coerce=int)
helmet = forms.TypedChoiceField(label='Do you have a helmet?', required=False, choices=choices, coerce=int)
nut = forms.TypedChoiceField(label='Do you have a set of nuts?', required=False, choices=choices, coerce=int)
hex = forms.TypedChoiceField(label='Do you have a set of hexes?', required=False, choices=choices, coerce=int)
# Misc.:
minibus = forms.TypedChoiceField(label='Can you drive the SU minibus?', required=False, choices=choices, coerce=int)
class Meta:
model = UserProfile
私のviews.py
def MemberRegistration(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/accounts/login/')
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.get(username=request.user)
profile = UserProfile(user=user)
profile.first_name = form.cleaned_data['first_name']
profile.last_name = form.cleaned_data['last_name']
profile.dob = form.cleaned_data['dob']
profile.stud_id = form.cleaned_data['stud_id']
profile.mob_num = form.cleaned_data['mob_num']
profile.home_num = form.cleaned_data['home_num']
profile.term_add = form.cleaned_data['term_add']
profile.perm_add = form.cleaned_data['perm_add']
profile.au_status = form.cleaned_data['au_status']
profile.nok_first_name = form.cleaned_data['nok_first_name']
profile.nok_last_name = form.cleaned_data['nok_last_name']
profile.relationship = form.clenaed_data['relationship']
profile.nok_mob_num = form.cleaned_data['nok_mob_num']
profile.nok_home_num = form.cleaned_data['nok_home_num']
profile.nok_add = form.cleaned_data['nok_add']
profile.diet = form.cleaned_data['diet']
profile.med_cond = form.cleaned_data['med_cond']
profile.fear = form.cleaned_data['fear']
profile.swim = form.cleaned_data['swim']
profile.sleeping_bag = form.cleaned_data['sleeping_bag']
profile.waterproof = form.cleaned_data['waterproof']
profile.shoes = form.cleaned_data['shoes']
profile.shoe_size = form.cleaned_data['shoe_size']
profile.harness = form.cleaned_data['harness']
profile.belay_device = form.cleaned_data['belay_device']
profile.helmet = form.cleaned_data['helmet']
profile.nut = form.cleaned_data['nut']
profile.hex = form.cleaned_data['hex']
profile.minibus = form.cleaned_data['minibus']
profile.save()
return render_to_response('base_profile.html', context_instance=RequestContext(request))
else:
return render_to_response('base_registration.html', {'form': form}, context_instance=RequestContext(request))
else:
form = RegistrationForm()
context = {'form': form}
return render_to_response('base_registration.html', context, context_instance=RequestContext(request))`
どんな助けでも大歓迎です!誰かがコードを削減する方法を教えてくれたら、それは素晴らしいことです。ご助力いただきありがとうございます :)
更新しました:
私はエラーが何であるか知っています!これを参照してください!モデルのユーザー フィールドに入力しませんでした。誰でもそれを手伝って、使用するコードの量を減らすことができますか?
更新 2:
フォームは渡されるようになりましたが、常にエラーが発生します。
このバージョンの views.py を使用するIntegrityError at /register/ - accounts_userprofile.first_name may not be NULL
と、行が表示されますprofile.save()
`if form.is_valid():
user = User.objects.get(username=request.user)
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
user.save()
profile = UserProfile(user=user,
dob = form.cleaned_data['dob'],
stud_id = form.cleaned_data['stud_id'],
mob_num = form.cleaned_data['mob_num'],
home_num = form.cleaned_data['home_num'],
term_add = form.cleaned_data['term_add'],
perm_add = form.cleaned_data['perm_add'],
au_status = form.cleaned_data['au_status'],
nok_first_name = form.cleaned_data['nok_first_name'],
nok_last_name = form.cleaned_data['nok_last_name'],
relationship = form.cleaned_data['relationship'],
nok_mob_num = form.cleaned_data['nok_mob_num'],
nok_home_num = form.cleaned_data['nok_home_num'],
nok_add = form.cleaned_data['nok_add'],
diet = form.cleaned_data['diet'],
med_cond = form.cleaned_data['med_cond'],
fear = form.cleaned_data['fear'],
swim = form.cleaned_data['swim'],
sleeping_bag = form.cleaned_data['sleeping_bag'],
waterproof = form.cleaned_data['waterproof'],
shoes = form.cleaned_data['shoes'],
shoe_size = form.cleaned_data['shoe_size'],
harness = form.cleaned_data['harness'],
belay_device = form.cleaned_data['belay_device'],
helmet = form.cleaned_data['helmet'],
nut = form.cleaned_data['nut'],
hex = form.cleaned_data['hex'],
minibus = form.cleaned_data['minibus'],
)
profile.save()`
代わりにこのバージョンの views.py を使用すると:
if form.is_valid():
profile = User.objects.get(username=request.user)
for key in UserProfileForm.base_fields.keys():
for val in form.cleaned_data.values():
setattr(profile, key, val)
profile.save()
return render_to_response('base_profile.html', context_instance=RequestContext(request))`
base_profile.html に渡され、first_name と last_name だけが term_add/perm_add/nok_add/diet/med_cond/fears フィールドのいずれかで埋められます。誰でも助けることができますか?初心者でごめんなさい >.<
User と UserProfile を一度に登録するために使用された私の古い views.py は、次のようになりました。
def MemberRegistration(request):
if request.user.is_authenticated():
return render_to_response('base_profile.html', context_instance=RequestContext(request))
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username = form.cleaned_data['username'],
email = form.cleaned_data['email'],
password = form.cleaned_data['password'],
)
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
member = user.save()
member = Member(user=user, first_name = form.cleaned_data['first_name'],
last_name = form.cleaned_data['last_name'],
date_of_birth = form.cleaned_data['date_of_birth'],
email = form.cleaned_data['email'],
student_id = form.cleaned_data['student_id'],
au_status = form.cleaned_data['au_status'],
mob_num = form.cleaned_data['mob_num'],
home_num = form.cleaned_data['home_num'],
term_time_add = form.cleaned_data['term_time_add'],
permanent_add = form.cleaned_data['permanent_add'],
diet = form.cleaned_data['diet'],
med_condition = form.cleaned_data['med_condition'],
fear = form.cleaned_data['fear'],
swim = form.cleaned_data['swim'],
sleeping_bag = form.cleaned_data['sleeping_bag'],
waterproof = form.cleaned_data['waterproof'],
shoes = form.cleaned_data['shoes'],
shoe_size = form.cleaned_data['shoe_size'],
harness = form.cleaned_data['harness'],
belay_device = form.cleaned_data['belay_device'],
helmet = form.cleaned_data['helmet'],
nuts_set = form.cleaned_data['nuts_set'],
hexes_set = form.cleaned_data['hexes_set'],
minibus_driver = form.cleaned_data['minibus_driver'],
)
member.save()
return render_to_response('base_profile.html', context_instance=RequestContext(request))
else:
return render_to_response('base_registration.html', {'form': form}, context_instance=RequestContext(request))
else:
form = RegistrationForm()
context = {'form': form}
return render_to_response('base_registration.html', context, context_instance=RequestContext(request))