我可以在模型字段中编辑标签吗?

时间:2017-01-28 15:52:59

标签: python html django django-models django-forms

myapp/models.py
class BookDetails(models.Model):
    user = models.ForeignKey(User)
    book_language = models.CharField(max_length=10, default='English')
    book_title = models.CharField(max_length=100, blank=False)
    book_author = models.CharField(max_length=100, blank=False)

的myapp / views.py

class BookDetailsForm(forms.ModelForm):
    class Meta:
        model = BookDetails
        fields = ('book_language', 'book_title', 'book_author',)

所以生成的html是:

<tr><th><label for="id_book_language">Book language:</label></th><td><input id="id_book_language" maxlength="10" name="book_language" type="text" value="English" required /></td></tr>
<tr><th><label for="id_book_title">Book title:</label></th><td><input id="id_book_title" maxlength="100" name="book_title" type="text" required /></td></tr>
<tr><th><label for="id_book_author">Book author:</label></th><td><input id="id_book_author" maxlength="100" name="book_author" type="text" required /></td></tr>

所需的HTML

<tr><th></th><td><input id="id_book_language" maxlength="10" name="book_language" type="text" value="English" placeholder='language'required /></td></tr>
<tr><th></th><td><input id="id_book_title" maxlength="100" name="book_title" type="text" placeholder='title' required /></td></tr>
<tr><th></th><td><input id="id_book_author" maxlength="100" name="book_author" type="text" placeholder='Author' required /></td></tr>

删除了标签代码并添加了占位符标记。

我知道我们可以通过here提到的自定义小部件来实现。我想只编辑现有的模型参数并添加这些标签。有可能吗?

0 个答案:

没有答案