Django模型

问题描述

我是Django的新手,我可以使用一些帮助。所以我正在尝试更改项目中的标签名称,但是我尝试了不同的方法却不起作用,这是最后一个

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'


               })

}

这是型号:

# 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,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',related_name="authenticationlevel")
    download_rights=models.CharField(max_length=500)
    
    
    def __str__(self):
        return self

这是我的观点:

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})

我也想在输入字段的头部对齐标签,我该怎么做? 预先谢谢你

解决方法

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'}),}