生成表单的短临时链接

时间:2018-05-03 09:13:02

标签: django

所以,我有一个登录用户可以填写的表单,当提交该表单时,我希望生成一个临时链接到另一个未登录的用户可以访问和填写的表单,会持续24小时。然后该表单将数据发送到数据库。该链接仅有几次有效。该号码由登录用户以第一种形式确定。

我希望该网址尽可能短,并且很容易在浏览器上手动记录,但我不知道最好的方法,甚至不知道如何操作。

第一个表单已完成,只有登录用户可以填写它,但我不知道如何生成该临时链接。

请记住,我是Django的新手。

Views.py

class GenFormPageView(LoginRequiredMixin, TemplateView):
login_url = '/'

def get(selfself, request, **kwargs):
    if request.method == 'POST':
        formation_form = genForm(data=request.POST)

        if formation_form.is_valid():
            cdata = formation_form.cleaned_data.get
        else:
            print('Invalid')
    else:
        formation_form = genForm()
        return render(request, 'genform.html', {'formation_form': formation_form})

Forms.py

class genForm(forms.ModelForm):
liste_formation = []
formations_info = {}
for formation in Formation.objects.all():
    if formation.libelle in formations_info:
        formations_info[formation.libelle].append(formation.formateur, formation.dateDebut, formation.dateFin,
                                                  formation.nbJours, formation.cours)
    else:
        info = {'formateur':formation.formateur.get_full_name(), 'dateDebut':formation.get_debut(), 'dateFin':formation.get_fin(), 'nbJours':formation.nbJours, 'cours':formation.cours.get_titre()}
        formations_info[formation.libelle] = info
    liste_formation.append(formations_info)

formations = [str(formation) for formation in Formation.objects.all()]

formation_select = forms.ChoiceField(label='Formation', choices=([(formation, formation) for formation in formations]), required = True)
formateur = forms.CharField(label='Formateur')
cours = forms.CharField(label='Cours')
nombre_eleves = forms.IntegerField(label='Nombre d\'élèves', min_value=1)

formations = json.dumps(formations_info)

class Meta:
    model = Formation
    fields = ('formation_select', 'formateur', 'dateDebut', 'dateFin', 'nbJours', 'cours', 'nombre_eleves')

urls.py

url(r'^genform/$', views.GenFormPageView.as_view()),

以下是确定新表格提交时间的字段

enter image description here

当我点击按钮' Generer'时,它会创建一个表单链接。该表单只能在固定的时间内提交,如果在生成时间的24小时内点击该链接,则链接只会导致该表单。

我现在正在尝试的是,在我的数据库中创建一个字段,其中包含生成表单的时间,然后我将检查时间是否有效,并仅在表单中显示该表单。

我现在的想法,但我仍然不知道怎么做。

1 个答案:

答案 0 :(得分:0)

所以,为了回答我的问题,我最终决定在我的数据库中使用包含表单创建日期的字段,然后我只检查今天的日期是否在我数据库中的日期之后的24小时内。< / p>