Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

django: valueerror on optional field

This is my first django project, I'm not sure why I'm getting an error 'The 'image2' attribute has no file associated with it.' when those fields are optional. How can I fix this?

model

class LinksContent(models.Model):
    author = models.ForeignKey(Author, on_delete=models.DO_NOTHING, default= True)
    title = models.CharField(max_length=100)
    somecontent = HTMLField(blank = True)
    image = models.ImageField(upload_to = 'images/%Y/%m/%d/')
    image2 = models.ImageField(upload_to = 'images/%Y/%m/%d/', blank = True)
    image3 = models.ImageField(upload_to = 'images/%Y/%m/%d/', blank = True)
    image4 = models.ImageField(upload_to = 'images/%Y/%m/%d/', blank = True)
    links = (
    ('faculties', 'faculties'),
    ('admission', 'admission'),
    ('resources', 'resources'),
    ('staff', 'staff'),
    ('research', 'research'),
    )
    file1 = models.FileField(upload_to = 'files/%Y/%m/%d/', blank = True)
    section = models.CharField(max_length=13, choices=links)
    is_published = models.BooleanField(default = True)
    publish_date = models.DateTimeField(default = datetime.now, blank = True)

html>

{% if content %}
    {% for item in content %}
        <div class="theHeader">
            <img class = 'header-img' src = "{{ item.image.url }}">
            <div class = 'author-info'>
                <img class = 'admin-img' src = "{{ item.author.image.url}}">
                <p>&nbsp;{{ item.author}} &nbsp;</p>
                <p>&nbsp;{{ item.publish_date | naturaltime}}</p>
            </div>
        </div>
        <div id="content">
            <h1>{{ item.title }}</h1>
            <p>{{ item.somecontent | safe }}</p>
            <div class = 'data'>
                <img class = 'images' src = "{{ item.image2.url }}">
                <img class = 'images' src = "{{ item.image3.url }}">
                <img class = 'images' src = "{{ item.image4.url }}">
            </div>
            <div class = 'data'>
                <p><a class = 'link' href = "{{ link.file1.url }}">Download Link</a></p>
            </div>
        </div>
    {% endfor %}
{% else %}
    <div>
        <p>No data available</p>
    </div>
{% endif %}
question from:https://stackoverflow.com/questions/65861508/django-valueerror-on-optional-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

blank=True means that your fields are optional during the the Form submission, if you want the field to optional in your Model during its creation you need to add null=True to the field in your Model like this:

image2 = models.ImageField(upload_to='images/%y/%m/%d/', null=True, blank=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...