Home > database >  Adding custom title to django form fields
Adding custom title to django form fields

Time:11-18

I would like to add custom title to one of my form fields. I made some modifications but it still not working.

My forms.py file

`

from django.forms import ModelForm
from django import forms
from .models import bug
from phonenumber_field.modelfields import PhoneNumberField


status_choice = [("Pending","Pending"),("Fixed","Fixed"),("Not Fixed","Not Fixed")]
class UploadForm(ModelForm):
    name = forms.CharField(max_length=200)
    info = forms.TextInput()
    status = forms.ChoiceField(choices = status_choice, widget= forms.RadioSelect())
    fixed_by = forms.CharField(max_length=30)
    phn_number = PhoneNumberField()
    #created_by = forms.CharField(max_length=30)
    #created_at = forms.DateTimeField()
    #updated_at = forms.DateTimeField()
    screeenshot = forms.ImageField()

    class Meta:
        model = bug
        fields = ['name', 'info', 'status', 'fixed_by', 'phn_number', 'screeenshot']
        labels = {'fixed_by' : 'Fixed by/Assigned to'}

`

my models.py file

`

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField, formfields
from django.utils import timezone
from django.contrib.auth.models import User

# Create your models here.
status_choice = [("Pending","Pending"),("Fixed","Fixed"),("Not Fixed","Not Fixed")]
class bug(models.Model):
    name = models.CharField(max_length=200, blank= False, null= False)
    info = models.TextField()
    status = models.CharField(max_length=25, choices=status_choice, default="Pending")
    fixed_by = models.CharField(verbose_name="Fixed by/Assigned to", max_length=30)
    phn_number = PhoneNumberField()
    user = models.ForeignKey(User, on_delete= models.CASCADE)
    created_at = models.DateTimeField(auto_now_add= True)
    updated_at = models.DateTimeField(auto_now= True)
    screeenshot = models.ImageField(upload_to='pics')

`

enter image description here

Need to change the form field title "Fixed by" to "Fixed by/Assigned to"

CodePudding user response:

In forms.py:

Just add label directly to form field so:

fixed_by = forms.CharField(max_length=30, label="Fixed by/Assigned to")
  • Related