Django:奇怪的反向比赛

时间:2017-11-23 17:59:38

标签: python django

在我的应用中,我有一个生成表单的视图。当此表单有效时,视图重定向到另一个视图,这是另一种形式,但我有一个反向匹配的错误消息。

我的views.py:

def uploadData(request, dataType, method):
    if method == 'single':
        if dataType == 'Sequence-has-SNP':
            if request.method == 'POST':
                form = SeqHasSnpForm(request.POST)
                if form.is_valid():
                    idSequence = form.cleaned_data['seq_linked']
                    return redirect('addSNPsSeq', idSequence)
            else:
                form = SeqHasSnpForm()
            return render(request, 'myapp/upload_sequence-has-snp.html', locals())
        else:
            ...
    else:
        ...


def uploadSNPsToSeq(request, idSequence):
    seq = Sequence.objects.get(PK_idSequence = idSequence)
    thisSeqHasSnp = Seq_has_SNP.objects.filter(FK_idSequence = seq.PK_idSequence)
    snpAll = SNP.objects.all()
    if request.method == 'POST':
        form = SelectSNPsForSeqForm(request.POST, snps=snpAll, seqHasSnps=thisSeqHasSnp)
        if form.is_valid():
            print('Yeaaahhh!')
    else:
        form = SelectSNPsForSeqForm(snps=snpAll, seqHasSnps=thisSeqHasSnp)
        print(form)
    return render(request, 'myapp/SNPs-to-add-to-sequence.html', locals())

my urls.py

urlpatterns = [
    url(r'^SNPs-to-add-to-sequence_(?P<idSequence>.+)$', views.uploadSNPsToSeq, name='addSNPsSeq'),
    url(r'^upload_(?P<dataType>[A-Za-z-]+)_(?P<method>(single|batch))$', views.uploadData, name='upload'),
    ...
    url(r'^$', views.home, name='home') 
]

我有两个名为upload_sequence-has-snp.htmlSNPs-to-add-to-sequence.html的模板。

这两种表单都可以,因为我可以访问我的第一个表单,例如选择form.cleaned_data['seq_linked']等于TEST。我可以在print(form)的{​​{1}}终端中看到结果,但此视图的最后一行会引发uploadSNPsToSeq

NoReverseMatch error

我不明白为什么要找NoReverseMatch at /myapp/SNPs-to-add-to-sequence_TEST Reverse for 'upload' with arguments '('', '')' not found. 1 pattern(s) tried: ['myapp/upload_(?P<dataType>[A-Za-z-]+)_(?P<method>(single|batch))$'] 代替myapp/upload_...

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

谢谢Alasdair!那就是问题。

因此,在我的views.py中,我将idSequence替换为dataType,将method添加为uploadSNPsToSeq函数的参数,并在urls.py中更改了网址格式这个观点。

views.py:

def uploadData(request, dataType, method):
    if method == 'single':
        if dataType == 'Sequence-has-SNP':
            if request.method == 'POST':
                form = SeqHasSnpForm(request.POST)
                if form.is_valid():
                    dataType = form.cleaned_data['seq_linked']
                    return redirect('addSNPsSeq', dataType, method)
            else:
                form = SeqHasSnpForm()
            return render(request, 'myapp/upload_sequence-has-snps.html', locals())
        else:
            ...
    else:
        ...

def uploadSNPsToSeq(request, dataType, method):
    seq = Sequence.objects.get(PK_idSequence = dataType)
    thisSeqHasSnp = Seq_has_SNP.objects.filter(FK_idSequence = seq.PK_idSequence)
    snpAll = SNP.objects.all()
    if request.method == 'POST':
        form = SelectSNPsForSeqForm(request.POST, snps=snpAll, seqHasSnps=thisSeqHasSnp)
        if form.is_valid():
            print('Yeaaahhh!')
    else:
        form = SelectSNPsForSeqForm(snps=snpAll, seqHasSnps=thisSeqHasSnp)
    return render(request, 'myapp/upload_snps-for-sequence.html', locals())

urls.py:

urlpatterns = [
    url(r'^upload_snps-for-sequence_(?P<dataType>[A-Za-z-]+)_(?P<method>(single|batch))$', views.uploadSNPsToSeq, name='addSNPsSeq'),
    url(r'^upload_(?P<dataType>[A-Za-z-]+)_(?P<method>(single|batch))$', views.uploadData, name='upload'),
    ...
    url(r'^$', views.home, name='home') 
]