I was studying alone through Jango Girls tutorial , and I wonder how to apply placeholder to the input/text area of the title and text attached below. If you share the code you are writing, it is as follows.
ps: self.fields['name'].They told me to apply widget.attrs.update({'class': 'special'})
but it's not easy because I have low basic understanding.
forms.py
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'text')
models.py
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
I slowly looked at the other answers in the stackoverflow I attached and applied them one by one and solved them. When it didn't work, I was in a hurry and tried again after a day, and it just worked. I added the following code to the code below and it worked. :-) Thank you.
widgets = {
'title': forms.TextInput(attrs={'placeholder': 'Name'}),
'text': forms.Textarea(
attrs={'placeholder': 'Enter description here'}),
}
forms.The full text of the py code.
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'text')
widgets = {
'title': forms.TextInput(attrs={'placeholder': 'Name'}),
'text': forms.Textarea(
attrs={'placeholder': 'Enter description here'}),
}
576 Who developed the "avformat-59.dll" that comes with FFmpeg?
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
922 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.