在Django中形成的其他帮助文本

时间:2016-04-08 14:12:12

标签: python django forms django-crispy-forms

当我创建或编辑model CV时,我需要在birth_date字段中输入一些数据。它有效,但我想添加一些额外的文字来定义一些日期格式,如(yyyy-mm-dd)。我使用cripsy表单来更好地查看表单。如何添加此帮助文本?

我的代码:

template.html

{% block profile %}

<div class="jumbotron">
    <h2>Edit your basic informations</h2>
    <hr>

    <form method="POST" class="post-form" enctype="multipart/form-data">
  {% csrf_token %}
    {{ form|crispy }}

        <button type="submit" class="save btn btn-default">Save</button>
    </form>
</div>



{% endblock %}

models.py

class Cv(models.Model):
    author = models.ForeignKey('auth.User')
    name = models.CharField(max_length=25, null = True)
    surname = models.CharField(max_length=25, null = True)
    city = models.CharField(max_length=100, blank=True)
    birth_date = models.DateField(blank=True, null=True)
    email = models.EmailField(max_length=50, null=True)
    main_programming_language = models.CharField(max_length=15, null = True)
    specialization = models.CharField(max_length=30, blank=True, null=True)
    interests = models.TextField(blank=True, null=True)
    summary = models.TextField(blank=True, null=True)
    #thumbnail = models.FileField(upload_to=get_upload_file_name, blank=True)


@property
def age(self):
    return int((datetime.datetime.now().date() - self.birth_date).days / 365.25 )

def zapisz(self):
    self.save()

def __str__(self):
    return self.surname.encode('utf-8')

forms.py

class CvForm(forms.ModelForm):

class Meta:
    model = Cv
    fields = ('name', 'surname', 'city', 'birth_date', 'email', 'main_programming_language', 'specialization', 'interests', 'summary',)

views.py

@login_required
def new_cv(request):
if request.method == "POST":
    form = CvForm(request.POST, request.FILES)

    if form.is_valid():
        cv = form.save(commit=False)
        cv.author = request.user
        cv.save()

        return redirect('proj.views.cv_detail', pk=cv.pk)
else:
    form = CvForm()
return render(request, 'new_cv.html', {'form': form})

1 个答案:

答案 0 :(得分:2)

可以将help_text添加到模型字段中:

birth_date = models.DateField(blank=True, null=True, help_text="format (yyyy-mm-dd)")

查看更多Django ModelForm文档。

您也可以使用外部库JQuery Tooltip