python - Django 表单图像选择 - 小部件类型
问题描述
行。我想制作一个从模型 A 更改图像的应用程序,并将其另存为模型 B。在模板中的表单上,我希望选择可见图像。我该怎么做?
型号和形式A
模型
from django.db import models
class Photo(models.Model):
photo = models.ImageField(blank=True, null=True)
形式
from django import forms
from .models import Photo
class Photoform(forms.ModelForm):
photo = forms.ImageField(required=True)
class Meta:
model = Photo
fields = ('photo',)
型号和形式 B
模型
from django.db import models
class Meme(models.Model):
meme = models.ImageField(blank=True, null=True)
形式
from django import forms
from Uploader.models import Photo
class Memeform(forms.Form):
meme = forms.ModelChoiceField(Photo.objects.all(), widget=forms.RadioSelect)
class Meta:
model = Photo
fields = ('meme',)
模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method= "post" enctype="multipart/form-data">
<p>
<h2>Your image here</h2>
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</p>
</form>
</body>
</html>
现在我有小部件 RadioSelect,但我需要选择可见的图像。
解决方案
form.fields["photo"].queryset = Photo.objects.all()
在您的意见中..您可以从这里参考更多.. http://www.wkoorts.com/wkblog/2009/08/10/pre-populate-django-modelform-with-specific-queryset/
推荐阅读
- c# - 在 DataGrid 中更改页面索引后,全局变量失去其值
- tensorflow - 创建新会话如何影响 TensorFlow 中变量的初始化?
- ios - 迅速; 来自 UNUserNotificationCenter 的 HTTP 请求不起作用:Internet 连接似乎处于脱机状态
- html-table - 使用 Handlebars 为表格内的按钮添加引导弹出窗口管理
- javascript - 如何取消这些 Promise?
- elasticsearch - 将 sql 文件导入 ElasticSearch
- java - ElasticSearch - matchPhraseQuery API 用于搜索多个字段
- c - 从字符串文字初始化的数组是否与从单个字符初始化的数组相同?
- ajax - Ajax数据表服务端处理绘制
- imgur - 调用上传图片时出现错误异常“未定义索引:请求”