gpt4 book ai didi

python - 最初在 Django-Form 中填充 FileField

转载 作者:行者123 更新时间:2023-11-28 18:28:47 24 4
gpt4 key购买 nike

我有一个描述网页的模型。 source_upload字段表示网页的屏幕截图。

为了向我的应用程序添加站点对象,我使用了基于 Django 类的 CreateView .这真的很好用。

现在我正在尝试添加一种半自动添加站点的方式。您可以将 URL 传递给 View , View 会自动填写表单(并制作网页的屏幕截图)。用户应该能够查看所有自动提取的字段 - 特别是自动生成的屏幕截图图像 - 更改它们并点击保存按钮以将对象添加到数据库并将图像(如果批准)添加到其最终位置。

我试图在 get_initial 中实现它 View 的方法。除了屏幕截图- FileField 外,这工作得很好.我在initial['source_upload']设置的路径未显示在 current: <link> 中表单的 FileInput 小部件的一部分。

如何给文件字段一个初始值?


模型.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'),

表单.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(用于创建和更新 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 %}

最佳答案

这是因为您的表单使用的 FileField 的值不仅仅是文件的路径 - 它是 FieldFile 的一个实例(参见 https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.fields.files.FieldFile)。

我不确定你是否可以直接实例化一个 FieldFile,但至少你可以通过实例化模型来做到这一点(你不需要保存它)。

在 views.py 中:

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

或者,您可以在呈现 html 时手动添加文件链接:

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

关于python - 最初在 Django-Form 中填充 FileField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39101231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com