简体   繁体   中英

Django: strange reverse match

In my app, I've got a view which generates a form. When this form is valid the view redirect to another view which is another form, but I have an error message with the reverse match.

My 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') 
]

I have my 2 templates called upload_sequence-has-snp.html and SNPs-to-add-to-sequence.html .

The 2 forms are OK because I have access to my first form and for example select a form.cleaned_data['seq_linked'] equal to TEST . I can see the result in the terminal of print(form) in uploadSNPsToSeq , but the last line of this view raise a 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))$']

I don't understand why is it looking for myapp/upload_... instead of myapp/SNPs-to-add-to-sequence_... ?

Any clue?

Thanks Alasdair! That was the issue.

So in my views.py I replaced idSequence by dataType , added method as parameters for my uploadSNPsToSeq function and in the urls.py changed the url pattern for this view.

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') 
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM