I am trying to create a form in which I want the user to give some option as an image and the user has to choose in b/w them but I have no idea how to do it I place an image in HTML show the user the image but I want to save that image option in my personal readme database also
here is my code
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
as you can see I want to give the user a system choice on by using a model which store the information like name image link and link of that system which they like to work on but instead of the name I want to give image option that why I am using image link so in my HTML I can view it with img src tag but unable to do it Any idea will helpful
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>
as you can see it's putting URL but I want to show image instead of url
views.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})
forms.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(),
}