Search code examples
djangodjango-formslabelmodelform

Django modelforms


i am new in Django and i could use some help. So i am trying to change the labels name in my project and it doesn't work i have tried in different ways and this is the last one:

from django import forms
from django.utils.translation import ugettext_lazy as _
from Upload_Page.models import Form_Data

class Upload_Document(forms.ModelForm):
     class Meta:

           model= Form_Data
           fields =['title','author','document_type','keywords','abstract','authentication','site','comments','file_upload']
           widgets = {
               'title' : forms.TextInput(attrs={
                   'class':'title_class',
                   'placeholder':'Insert title here',
                   'label':'Tit'


               })

}

This are the models:

# Create your models here.
class Form_Data(models.Model):
    title=models.CharField(unique=True,max_length=100,blank=False)
    author=models.CharField(max_length=100)
    document_type=models.CharField(choices=DOCUMENT_TYPES,max_length=500,blank=False,default=None)
    keywords=models.CharField(max_length=500)
    abstract=models.TextField(null=True,blank=True)
    authentication=models.CharField(choices=DOCUMENT_AUTHENTICATION_LEVEL,unique=True,blank=False,default=None,max_length=500)
    site=models.URLField(unique=True,blank=True)
    comments=models.TextField(null=True,blank=True)
    file_upload=models.FileField(default=None)


    def __str__(self):
        return self.title
    
class Authentication_Level(models.Model):
    title=models.ForeignKey('Form_Data',to_field='title', on_delete=models.CASCADE,related_name='title1')
    authentication=models.ForeignKey('Form_Data',to_field='authentication', on_delete=models.CASCADE, related_name="authenticationlevel")
    download_rights=models.CharField(max_length=500)
    
    
    def __str__(self):
        return self

and this is my view:

from django.shortcuts import render
from django.views.generic import View
from Upload_Page import models
from django.core.files.storage import FileSystemStorage
from Upload_Page.forms import Upload_Document
from django.shortcuts import redirect



def upload_doc(request):
  if request.method == 'POST':
    form = Upload_Document(request.POST, request.FILES)
    if form.is_valid():
      form.save()
      return redirect('home_page:homepageview')
  
  else:
    form = Upload_Document(auto_id=True,label_suffix='')
  return render(request, 'Upload_Page/upload_page.html', {'form':form})

Also i want to align the label in the head of the inputs fields, how can i do that? Thank you in advance


Solution

  • class Upload_Document(forms.ModelForm):
        class Meta:
            model = Form_data
    
            labels = {
                    "title": "Insert Desired Label Here",
                    "author": "Insert Desired Label Here",
                    "document_type": "Insert Desired Label Here",
                     // etc etc etc...
            }
    
            fields = ['title', 'author', 'document_type'] // etc etc etc...
    
    
     // To align things you can use css in the widgets section:
           
            widgets = {
                    'document_type': forms.Textarea(attrs={'style': 'vertical-align: text-top'}),
            }