我正在使用https://bixly.com/blog/awesome-forms-django-crispy-forms/,尝试使用django crispy表单设置bootstrap 3表单。
在app1 / models.py中,我已经设置了我的模型:
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django import forms
class User(AbstractUser):
# Address
contact_name = models.CharField(max_length=50)
contact_address = models.CharField(max_length=50)
contact_email = models.CharField(max_length=50)
contact_phone = models.CharField(max_length=50)
......
APP1 / forms.py:
class UserForm(forms.ModelForm):
class Meta:
model = User # Your User model
fields = ['contact_name', 'contact_address', 'contact_email', 'contact_phone']
helper = FormHelper()
helper.form_method = 'POST'
helper.form_action = "/contact/"
helper.form_id = 'form' # SET THIS OR BOOTSTRAP JS AND VAL.JS WILL NOT WORK
helper.add_input(Submit('Submit', 'Submit', css_class='btn-primary'))
APP1 / views.py:
def contact(request):
django_query_dict = request.POST
message = django_query_dict.dict()
if request.method == 'POST':
print "im in contact "+str(message)
form = UserForm(request.POST)
if form.is_valid():
form.save()
else:
print('invalid')
我已经能够让它在第一张唱片中工作(截图如上)。但是,由于我使用/扩展用户模型,我遇到了内置用户注册功能的问题。当我尝试添加第二条记录时,我得到了:
Exception Type: IntegrityError at /contact/
Exception Value: column username is not unique
Request information:
GET: No GET data
我可以看到这是真的,因为我没有在表单中获取用户名的数据,因此第一次和第二次记录都会输入一个空白,导致第二次出现完整性错误。我怎样才能解决这个问题。我是否应该使用内置的用户模型?