проблема в том, что при первой загрузке формы(когда пользователь ещё ничего не вводил и ничего не отправлял) около обязательного к заполнению поля(title) выводится надпись “Обязательное поле”. но эта надпись должна выводится только после того как пользователь, не заполнив поле, сделает попытку отправить форму
views.py:
@login_required def path_glory_add_item(request): if request.method == 'POST': form = PathGloryForm(request.POST, request.FILES) if form.is_valid(): PathGlory( user_id=form.cleaned_data.get('user_id'), title=form.cleaned_data.get('title').strip(), date=form.cleaned_data.get('date'), place=form.cleaned_data.get('place').strip(), teaser=form.cleaned_data.get('teaser').strip(), text=form.cleaned_data.get('text').strip(), path_glory_photo=form.cleaned_data.get('path_glory_photo'), ).save() return HttpResponseRedirect('/userprofile/path_glory_add_item_added/') else: form = PathGloryForm({ 'user_id': request.user.pk, }) t = loader.get_template('path_glory_add_item.html') c = RequestContext(request, { 'form': form, }, [custom_proc]) return HttpResponse(t.render(c))
forms.py:
class PathGloryForm(forms.ModelForm): date = forms.DateField( widget=forms.TextInput(attrs={ 'class':'datepicker', }), required=False, label='Дата проведения', ) user_id = forms.IntegerField( widget=forms.HiddenInput, required=True, ) class Meta: model = PathGlory fields = ( 'user_id', 'title', 'date', 'place', 'teaser', 'text', 'path_glory_photo', )
models.py:
class PathGlory(models.Model): user_id = models.IntegerField() #это поле очень важное. оно нужно title = models.CharField( 'Название события', max_length=100, blank=False, ) date = models.DateField( 'Дата проведения', blank=True, null=True, ) place = models.CharField( 'Место проведения', max_length=200, blank=True, null=True, ) teaser = models.TextField( 'Вступительный текст (тизер)', max_length=10000, blank=True, null=True, help_text='Будет отображаться в ленте', ) text = models.TextField( 'Основной текст', max_length=100, blank=True, null=True, help_text='Будет отображаться на отдельной странице', ) path_glory_photo = models.ImageField( 'Фото', blank=True, upload_to='userprofile/path_glory_photo/', null=True, )