【发布时间】:2022-10-25 18:20:46
【问题描述】:
我正在尝试创建一个表单,我希望用户在其中提供一些选项作为图像,并且用户必须以黑白方式选择它们,但我不知道该怎么做我将图像放在 HTML 中向用户显示图像,但我也想将该图像选项保存在我的个人自述数据库中
这是我的代码
class SystemChoice (models.Model):
name = models.CharField(max_length=200)
img_link = models.URLField(blank=False)
link = models.URLField(blank=False)
def __str__(self):
return self.img_link
class Personal_readme(models.Model):
system_choice = [
('windows', 'windows'),
('linux', 'linux'),
('macOs', 'macOs'),
('unix', 'unix')
]
work_status_Regex = RegexValidator(regex = "((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)")
name = models.CharField(max_length=70, blank=False)
about_me = models.CharField(max_length=100, blank=True)
work_status = models.CharField(max_length=70, blank=True)
work_status_link = models.URLField(validators = [work_status_Regex], blank=True)
system = MultiSelectField(max_length=20, choices=system_choice,max_choices=4, blank=True )
def __str__(self):
return self.name
如您所见,我想通过使用模型为用户提供系统选择,该模型存储诸如名称图像链接和他们喜欢使用的系统的链接之类的信息,而不是我想提供图像选项的名称,这是为什么我正在使用图像链接,因此在我的 HTML 中我可以使用 img src 标签查看它,但无法执行 任何想法都会有所帮助
HTML
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.name|as_crispy_field }}
{{ form.about_me|as_crispy_field }}
{{ form.work_status|as_crispy_field }}
{{ form.work_status_link|as_crispy_field }}
<img src="{{ form.system|as_crispy_field }}" alt=""> <input type="submit" value="Genrate File">
</form>
如您所见,它正在放置 URL,但我想显示图像而不是 url
视图.py
def home(request):
if request.method == 'POST':
form = Personal_Readme_form(request.POST)
if form.is_valid():
form.save()
return redirect('request:preview')
else:
form = Personal_Readme_form()
return render(request, 'home.html', {'form': form})
表格.py
class Personal_Readme_form(forms.ModelForm):
class Meta:
model = Personal_readme
fields = '__all__'
labels = {
'name':'Your Name',
'about':'About Yourself',
'work_status':'Your Current work status',
'resume_link':'Your Resume',
'work_status':'Your current status',
'system':'I prefer working on',
}
widgets = {
'name': forms.TextInput(attrs={'placeholder': 'Type your name'}),
'about_me': forms.Textarea(attrs={'placeholder': 'A short summary about yourself'}),
'project1': forms.TextInput(attrs={'placeholder':'Name of your project'}),
'project2': forms.TextInput(attrs={'placeholder':'Name of your project'}),
'project3': forms.TextInput(attrs={'placeholder':'Name of your project'}),
'project4': forms.TextInput(attrs={'placeholder':'Name of your project'}),
'project5': forms.TextInput(attrs={'placeholder':'Name of your project'}),
'work_status' : forms.TextInput(attrs={'placeholder': 'Your current status'}),
'system' : forms.CheckboxSelectMultiple(),
}
【问题讨论】:
标签: python django django-models django-views django-forms