Simon Lenz
Simon Lenz

Reputation: 2812

Initially populate FileField in Django-Form

I have a model that describes a Webpage. The source_upload field represents a screenshot of the webpage.

For adding site-objects to my application, I use a django class-based CreateView. This works really well.

Now I'm trying to add a semi-automatic way of adding sites. You can pass an URL to the view and the view fills the form automatically (and makes a screenshot of the webpage). The user should be able to review all the auto-extracted fields - especially the auto generated screenshot image - change them and hit the save button to add the object to the database and the image (if approved) to its final location.

I tried to implement this in the get_initial method of the view. This works quite well except for the screenshot-FileField. The path I set in initial['source_upload'] is not shown in the current: <link>part of the FileInput widget of the form.

How can I give the filefield an initial value?


models.py

class Site(models.Model):

    def get_source_upload_path(instance, filename):
        now = datetime.datetime.now()
        return "appname/sites/{}/{}/{}/site_{}_{}".format(now.year, now.month, now.day, instance.pk, filename)

    creationDate = models.DateTimeField(auto_now_add=True)
    last_modifiedDate = models.DateTimeField(auto_now=True)
    creator = models.ForeignKey('auth.User', related_name='siteCreated')
    last_modifier = models.ForeignKey('auth.User', related_name='siteLast_modified')

    date = models.DateTimeField(default=datetime.date.today)    
    title = models.CharField(max_length=240, blank=True)
    body = models.TextField(max_length=3000)

    source_url = models.URLField(blank=True)
    source_upload = models.FileField(upload_to=get_source_upload_path, blank=True)

    keywords = models.ManyToManyField("Keyword")

urls.py

url(r'site/add/$', views.SiteCreate.as_view(), name='site-add'),
url(r'site/add/(?P<source_url>[A-Za-z0-9\-._~:/\[\]@!$&\'\(\)\*\+,;=?#]+)/$', views.SiteCreate.as_view(), name='site-add-fromurl'),

forms.py

class SiteForm(ModelForm):
    class Meta:
        model = Site
        fields = ['date', 'title', 'body', 'source_url', 'source_upload', 'keywords']
        widgets = {
            'keywords' : CheckboxSelectMultiple(),
        }

views.py

class SiteCreate(LoginRequiredMixin, CreateView):
    model = Site
    template_name = 'appname/site_form.html'
    form_class = SiteForm
    success_url = reverse_lazy('appname:index')

    def form_valid(self, form):
        form.instance.creator = self.request.user
        form.instance.last_modifier = self.request.user
        return super(SiteCreate, self).form_valid(form)

    def get_initial(self):
        # Get the initial dictionary from the superclass method
        initial = super(SiteCreate, self).get_initial()

        try:
            #get target url from request
            fullpath = self.request.get_full_path()
            fullpath = fullpath.split("/")
            fullpath, querystring = fullpath[3:-1], fullpath[-1]
            source_domain = fullpath[2]
            fullpath = "/".join(fullpath)
            fullpath += querystring

            source_url = fullpath

            if (not source_url.startswith("http://") and not source_url.startswith("https://")):
                print("ERROR: url does not start with http:// or https://")
                return initial

            # ...
            # extract title, date & others with BeautifulSoup
            # ...

            #extract screenshot  (is there a better way?)
            from selenium import webdriver
            driver = webdriver.Firefox()
            driver.get(source_url)
            tmpfilename = "{}_{}.png".format(get_valid_filename(source_domain), get_valid_filename(title[:30]))
            now = datetime.datetime.now()
            tmpfilepath_rel =  "appname/sites/tmp/{}/{}/{}/{}".format(now.year, now.month, now.day, tmpfilename)
            tmpfilepath = settings.MEDIA_ROOT + tmpfilepath_rel

            folder=os.path.dirname(tmpfilepath)
            if not os.path.exists(folder):
                os.makedirs(folder)
            driver.save_screenshot(tmpfilepath)
            driver.quit()

            initial = initial.copy()
            initial['source_url'] = source_url
            initial['title'] = title
            initial['date'] = soup_date
            initial['body'] = body
            initial['source_upload'] = tmpfilepath_rel
        except KeyError as e:
            print("no valid source_url found. zeige also ganz normales add/new template")
        except IndexError as e:
            print("no valid source_url found. zeige also ganz normales add/new template")

        return initial

site_form.html (Used for Create and Update view)

{% extends "appname/base.html" %}
{% load staticfiles %}
{% block header %}
    <link rel="stylesheet" type="text/css" href="{% static 'appname/model_forms.css' %}" />
{% endblock %}
{% block body %}
        <form enctype="multipart/form-data" action="" method="post">{% csrf_token %}

            <div class="fieldWrapper">
                <div class="error">{{ form.date.errors }}</div>
                <div class="label">{{ form.date.label_tag }}</div>
                <div class="field">{{ form.date }}<br />{{ form.date.help_text }}</div>
                <div class="floatclear"></div>
            </div>
            <div class="fieldWrapper">
                <div class="error">{{ form.title.errors }}</div>
                <div class="label">{{ form.title.label_tag }}</div>
                <div class="field">{{ form.title }}<br />{{ form.title.help_text }}</div>
                <div class="floatclear"></div>
            </div>
            <div class="fieldWrapper">
                <div class="error">{{ form.body.errors }}</div>
                <div class="label">{{ form.body.label_tag }}</div>
                <div class="field">{{ form.body }}<br />{{ form.body.help_text }}</div>
                <div class="floatclear"></div>
            </div>
            <div class="fieldWrapper">
                <div class="error">{{ form.source_url.errors }}</div>
                <div class="label">{{ form.source_url.label_tag }}</div>
                <div class="field">{{ form.source_url }}<br />{{ form.source_url.help_text }}</div>
                <div class="floatclear"></div>
            </div>
            <div class="fieldWrapper">
                <div class="error">{{ form.source_upload.errors }}</div>
                <div class="label">{{ form.source_upload.label_tag }}</div>
                <div class="field">{{ form.source_upload }}<br />{{ form.source_upload.help_text }}</div>
                <div class="floatclear"></div>
            </div>
            <div class="fieldWrapper">
                <div class="error">{{ form.keywords.errors }}</div>
                <div class="label">{{ form.keywords.label_tag }}</div>

                <div class="field">
                    <ul class="checkbox-grid">
                        {% for kw in form.keywords %}
                            <li>
                                {{ kw.tag }}
                                <label for="{{ kw.id_for_label }}">
                                    {{ kw.choice_label }}
                                </label>
                            </li>
                        {% endfor %}
                    </ul>
                    <div class="checkbox_help_text"><br />{{ form.keywords.help_text }}</div>
                </div>
                <div class="floatclear"></div>
            </div>
            <input type="submit" value="Save" />
        </form>
        <div id="ObjectHistory">
            {% if site.pk %}
                <p>Created by: {{ site.creator }}</p>
                <p>Created on: {{ site.creationDate }}</p>
                <p>Last modified by: {{ site.last_modifier }}</p>
                <p>Last modified on: {{ site.last_modifiedDate }}</p>
                <p>Now: {% now "Y-m-d H:i:s" %} <a href="{% url 'appname:site-delete' site.pk %}"><button>delete</button></a></p>
            {% else %}
                <p>This is a new Site!</p>
                <p>Now: {% now "Y-m-d H:i:s" %}</p>
            {% endif %}
        </div>
{% endblock %}

Upvotes: 5

Views: 5020

Answers (1)

skoll
skoll

Reputation: 2452

This is because the value of FileField, as used by your form, isn't just the path to the file - it's an instance of FieldFile (see https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.fields.files.FieldFile).

I'm not sure if you can instantiate a FieldFile directly, but at least you can do it by instantiating the model (you don't need to save it).

In views.py:

tmp_site = Site(source_upload=tmpfilepath_rel)
initial['source_upload'] = tmp_site.source_upload

Alternatively you can manually add a link to the file when rendering the html:

<div class="currently"><a href="{{ form.source_upload.value }}">{{ form.source_upload.value }}</a></div>

Upvotes: 1

Related Questions