У меня есть такая модель:
models.py
from django.db import models
class Foo(models.Model):
text = models.TextField()
Примеры экземпляров этой модели:
Foo.objects.create(text="My first text [[@shorttext_1@]] random text.")
Foo.objects.create(text="Select something from below [[@multipleselect_1@]]. text.")
Foo.objects.create(text="A different form [[@shorttext_1@]] and another"
"form [[@shorttext_2@]] random texts.")
Foo.objects.create(text="Mixed form [[@shorttext_1@]] and another"
"form [[@multipleselect_1@]] random text.")
Значения [[@shorttext_1@]]
, [[@multipleselect_1@]]
представляют местоположение и тип форм для размещения в шаблоне ниже.[[@ @]]
- это случайно выбранный заполнитель стиля уценки.
forms.py
from django import forms
class ShortTextForm(forms.Form): # [[@shorttext_1@]] form
short_text = forms.CharField(max_length=300)
class MultipleSelectionForm(forms.Form): # [[@multipleselect_1@]] form
selection = forms.ChoiceField(
choices=[('A', 'A text'), ('B', 'B text')],
widget=forms.RadioSelect())
views.py
from django.shortcuts import render
def text_view(request):
if request.method == 'POST':
# get the info for each form
else:
foo = Foo.objects.order_by('?').first()
return render(
request=request,
template_name='templates/index.html',
context={'text': foo.text})
templates / index.html
{% extends "base.html" %}
{% block body %}
{{ text }}
{% endblock %}
Можно ли отобразить foo.text
в шаблоне и отобразить нужные формы?
В настоящее время в моем классе Foo
есть переменная type
обозначить тип формы.И мой взгляд может отображать только один желаемый тип формы, который может быть размещен только в конце текста.Я хочу сделать несколько форм в любом месте текста, используя только один шаблон.
РЕДАКТИРОВАТЬ:
Чтобы привести пример выходных данных, которые я хочу достичь:
Foo.objects.create(text="The age of the person is [[@shorttext_1@]] and "
"another attribute is [[@shorttext_2@]]. "
"Additionally select one:<br>[[@multipleselect_1@]]")
этот объект должен быть представлен в шаблоне так, чтобы выходные данные выглядели так:
![enter image description here](https://i.stack.imgur.com/pqIEw.png)