我需要一个html格式的选项,其中只能选择一个图像(某种无线电类型),这些图像以水平对齐方式显示。
我的老年人使用Django ModelForm和基于类的视图来完成此操作,但是我创建的视图是基于函数的,并且我使用的是html表单而不是django表单。老年人。
class Persona(models.model):
name = models.CharField()
image = models.ForeignKey(Image,on_delete=models.CASCADE,limit_to_choices={'choice_model':'profile'},)
def __str__(self):
return self.name
class Image(models.Model):
image =models.CharField(max_length=120,null=True)
choice_model =models.CharField(max_length=120)
def __str__(self):
return self.image
class HorizontalRadioSelect(forms.RadioSelect):
template_name = 'dashboard/horizontal_select.html'
class CustomChoiceField(forms.ModelChoiceField):
def label_from_instance(self,obj):
return mark_safe("<img src='%(media_url)simages/%(image)s' width=50 height=50/> " % {'media_url':settings.MEDIA_URL,'image':obj.image})
class PersonaForm(forms.ModelForm):
persona_name = forms.CharField(label='Persona Name', max_length=100)
image = CustomChoiceField(widget=HorizontalRadioSelect(), queryset=Image.objects.filter(choice_model="profile"),initial='1')
class PersonaCreateView(LoginRequiredMixin,TemplateView):
form_class = PersonaForm
template_name = "dashboard/c_persona.html"
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {"form": form})
def post(self,request):
form = PersonaForm(data=request.POST)
if form.is_valid():
persona = form.save()
else:
print(form.errors)
return HttpResponseRedirect('/persona/')
<form method="get">
<div>
</i><strong>Enter Persona Name</strong>:   <input type="text" name="persona_name" , required="",>
</i><strong>Select Image</strong>: 'I need your help here.I need a horizontal options of images from which i can select only one image'
<input type="submit" name="Save">
</form>
def CreatePersonaView(request):
p_name = request.GET.get('persona_name')
p_image = #I don't know what to do here as i haven't written code for this in html form.But i want to do this task from GET request.
persona = Persona(name=p_name,image=p_image)
persona.save()
return render('request', 'dashboard/c_persona.html')