[Answered ]-Django template/view issues with carousel

2👍

✅

The .py files are Python code. In Python, leading whitespace matters.

In Views.py, the carousel function does not contain valid Python code:

def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')
else:

 form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
#documents=DocumentForm().
# Render list page with the documents and the form
return render_to_response(
    'webportal/index.html',
    {'documents': documents, 'form': form,},
    context_instance=RequestContext(request)
)

Instead, it should probably be:

def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')

    else:

        form = DocumentForm() # A empty, unbound form
        # Load documents for the list page
        documents = Document.objects.all()
        #documents=DocumentForm().
        # Render list page with the documents and the form
        return render_to_response(
            'webportal/index.html',
            {'documents': documents, 'form': form,},
            context_instance=RequestContext(request)
        )

But it is not clear if the else part belongs to the first or the second if construct.

Leave a comment