【问题标题】:Django: How to Add Default Location for Forms.ImageFieldDjango:如何为 Forms.ImageField 添加默认位置
【发布时间】:2021-06-22 21:13:27
【问题描述】:

我正在将我的模型更改为表单,以便我可以为我的文本框使用占位符。在这样做时,我很难更改ImageField,因为它向我提供了一个错误,它收到了“一个意外的关键字:默认”

这是我的models.py:

class Project(forms.Form):
    name = forms.CharField(max_length=30)
    #owner = models.ForeignKey(User, on_delete=models.CASCADE, null = True)
    bPic = forms.ImageField(default='defaultproban.jpg', upload_to='project_banner')
    logo = forms.ImageField(default='defaultlogo.jpg', upload_to='project_logo')
    dep_choice1 = (
        ('Behavioral Sciences and Leadership', ('Behavioral Sciences and Leadership')),
        ('Chemistry and Life Science', ('Chemistry and Life Science')),
        ('Civil and Mechanical Engineering', ('Civil and Mechanical Engineering')),
        ('Electrical Engineering and Comptuer Science', ('Electrical Engineering and Comptuer Science')),
        ('English and Philosophy', ('English and Philosophy')),
        ('Foreign Languages', ('Foreign Languages')),
        ('Geography and Environmental Engineering', ('Geography and Environmental Engineering')),
        ('History', ('History')),
        ('Law', ('Law')),
        ('Mathematical Sciences', ('Mathematical Sciences')),
        ('Physics and Nuclear Engineering', ('Physics and Nuclear Engineering')),
        ('Social Sciences', ('Social Sciences')),
        ('Systems Engineering', ('Systems Engineering')),
        ('Independent', ('Independent')),
    )
    department = forms.CharField(
        max_length=50,
        choices=dep_choice1,
        default='Independent',
    )
    purpose = forms.CharField(max_length=50, null = True, widget=forms.TextInput(attrs={'placeholder':'Write Your Mission Statement'}))
    description=forms.TextField(widget=forms.TextInput(attrs={'placeholder':'Briefly Describe Your Project, Progress, Goals, and Your Team!'}))
    tag_choice = (
        ('Data Analysis' , ('Data Analysis')),
        ('3D Printing' , ('3D Printing')),
        ('Robotics' , ('Robotics')),
        ('Coding' , ('Coding')),
        ('Science' , ('Science')),
        ('Drones' , ('Drones')),
        ('Math' , ('Math')),
        ('Chemistry' , ('Chemistry')),
        ('Nuclear Engineering' , ('Nuclear Engineering')),
        ('Physics' , ('Physics')),
        ('Photonics' , ('Photonics')),
        ('MATLAB' , ('MATLAB')),
        ('SolidWorks' , ('SolidWorks')),
        ('Writing' , ('Writing')),
        ('Graphic Design' , ('Graphic Design')),
        ('Design' , ('Design')),
        ('Robotics' , ('Robotics')),
        ('Business' , ('Business')),
        ('Stocks' , ('Stocks')),
        ('Hacking' , ('Hacking')),
        ('Law' , ('Law Studies')),
        ('Coding' , ('Coding')),
        ('Environmental' , ('Environment')),
        ('Lifestyle' , ('Lifestyle')),
        ('Kinesiology' , ('Kinesiology')),
        ('Health' , ('Health')),
        ('Sleep' , ('Sleep')),
        ('Psychology' , ('Psychology')),
        ('Material Science' , ('Material Science')),
        ('Batteries' , ('Batteries')),
        ('Energy' , ('Energy')),
        ('Fiber Optics' , ('Fiber Optics')),
        ('Space' , ('Space')),
        ('Autonomous Vehicles' , ('Autonomous Vehicles')),
        ('Biology' , ('Biology')),
    )

    projectTag = forms.CharField(
        max_length=32,
        choices=tag_choice,
        default='Frauds',
    )

    look = (
        ('Expert Cadets.', ('motivated cadets with niche expertise.')),
        ('Any cadet who wants to help.', ('cadets who want to learn and help.')),
        ('an engineering cadet.', ('an engineering Cadet')),
        ('a cadet with a scientific background.', ('a cadet with a scientific background')),
        ('cadets with programming experience.', ('cadets with programming experience')),
        ('Stem interests.', ('cadets with an interest in STEM.')),
        ('scholarship', ('cadets seeking scholarships from research')),
        ('a lot of work.', ('cadets who can work 10-20 hours a week')),
        ('anyone', ('anyone!')),
    )
    
    lookingFor = forms.CharField(
        max_length=75,
        choices=look,
        default='an engineering cadet,',
    )

    recruit = (
        ('Yes', ('Yes')),
        ('No', ('No')),
    )

    recruiting = forms.CharField(
        max_length=50,
        choices=recruit,
        default='Yes',
    )
    statusOptions = (
        ('Active', ('Active')),
        ('Archived', ('Archived')),
        ('Deleted', ('Deleted')),
    )

    status = forms.CharField(
        max_length=50,
        choices=statusOptions,
        default='Active',
    )
    class Meta:
        verbose_name_plural= "projects"

    def __str__(self):
        return self.name

谢谢,如果代码的任何其他部分对您有帮助,请告诉我,但这应该很好。

【问题讨论】:

    标签: python django forms model


    【解决方案1】:

    您不能使用 default 关键字或 upload_to,这不是模型。

    如果你想保存图片,你应该使用模型并在那里指定名称和上传位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多