В моем проекте django у меня есть форма для управления регистрацией пользователей, здесь мой код:
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,)
u_fullname = models.CharField(max_length=200)
u_email = models.EmailField()
u_profile = models.CharField(max_length=1)
u_type = models.CharField(max_length=1, default='F')
u_job = models.CharField(max_length=100, null=True, blank=True, default='D')
u_country = models.CharField(max_length=20, null=True, blank=True, default='Italy')
u_regdata = models.DateTimeField(auto_now=True)
u_picture = models.ImageField(upload_to='profile_images', blank=True)
u_active = models.BooleanField(default=False)
u_terms = models.BooleanField(default=False)
def __unicode__(self):
return self.u_profile
forms.py
class ProfileModelForm(ModelForm):
class Meta:
model = UserProfile
fields = ['u_fullname',
'u_job',
'u_country',
'u_email',
'u_terms',
]
def clean(self):
cleaned_data = super(ProfileModelForm, self).clean()
u_fullname = cleaned_data.get('u_fullname')
u_job = cleaned_data.get('u_job')
u_country = cleaned_data.get('u_country')
u_email = cleaned_data.get('u_email')
u_terms = cleaned_data.get('u_terms')
if not u_terms:
raise forms.ValidationError("Please read and accept our Terms of Service")
return u_terms
if not u_fullname and not u_job and not u_country and not u_terms:
raise forms.ValidationError('You have to write something!')
views.py
if request.method == 'POST':
if 'user_reg' in request.POST:
form = ProfileModelForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
#Create user and get the id
n_user = User.objects.create_user(username=request.POST['u_email'],
email=request.POST['u_email'],
password=request.POST['u_password'])
instance.user = User.objects.get(id=n_user.id)
instance.u_profile = 'U'
instance.save()
return
else:
messages.error(request, "Error")
в конце моей html части формы:
<form action="" method="POST">
{% csrf_token %}
{{ form.errors }}
<div class="row">
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="text" name="u_fullname" placeholder="Full Name" value={{ form.u_fullname }}>
<i class="la la-user"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<select value={{ form.u_country }}>
<option selected="selected">Italy</option>
<option>Spain</option>
<option>USA</option>
<option>France</option>
</select>
<i class="la la-globe"></i>
<span><i class="fa fa-ellipsis-h"></i></span>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<select value={{ form.u_job }}>
<option value="D" selected="selected">Developer</option>
<option value="T">Tester</option>
<option value="S">System Administrator</option>
<option value="V">Sales</option>
</select>
<i class="la la-dropbox"></i>
<span><i class="fa fa-ellipsis-h"></i></span>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="text" name="u_email" placeholder="Enter a valid email" value="{{ form.u_email }}">
<i class="la la-envelope"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="password" name="u_password" placeholder="Password">
<i class="la la-lock"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="sn-field">
<input type="password" name="repeat-password" placeholder="Repeat Password">
<i class="la la-lock"></i>
</div>
</div>
<div class="col-lg-12 no-pdd">
<div class="checky-sec st2">
<div class="fgt-sec">
<input type="checkbox" name="cc" id="c2" value={{ form.u_terms }}>
<label for="c2">
<span></span>
</label>
<small>Yes, I understand and agree to the workwise Terms & Conditions.</small>
</div><!--fgt-sec end-->
</div>
</div>
<div class="col-lg-12 no-pdd">
<button type="submit" name="user_reg" value="submit">Get Started</button>
</div>
</div>
</form>
хорошо, теперь моя проблема в том, что для введенных данных и полей показываются не принимать во внимание; каждый выбор, который я делаю или также, если я отмечаю флажок в базе данных, я нахожу когда-либо значения модели по умолчанию. Как я могу решить мою проблему для правильного управления всеми полями в моей форме?
Огромное спасибо заранее