I have the following function in Django to download the Fasta sequences (protein) through the selected categories as a file. This works well. I would like to improve the following script.

import textwrap
from io import StringIO
from django.shortcuts import render
from django.http import HttpResponse
from database.models import PesticidalProteinDatabase

def download_data(request):
    """List the categories for download."""

    categories = \
    category_prefixes = []
    for category in categories:
        prefix = category[0][:3]
        if prefix not in category_prefixes:

    context = {
        'proteins': PesticidalProteinDatabase.objects.all(),
        'category_prefixes': category_prefixes
    return render(request, 'database/download_category.html', context)

def download_category(request, category=None):
    """Download the fasta sequences for the category."""

    context = {
        'proteins': PesticidalProteinDatabase.objects.all()
    category = category.title()
    file = StringIO()
    data = list(context.get('proteins'))

    for item in data:
        if category in item.name:
            fasta = textwrap.fill(item.fastasequence, 80)
            str_to_write = f">{item.name}\n{fasta}\n"

    if 'All' in category:
        for item in data:
            fasta = textwrap.fill(item.fastasequence, 80)
            str_to_write = f">{item.name}\n{fasta}\n"

    response = HttpResponse(file.getvalue(), content_type="text/plain")
    download_file = f"{category}_fasta_sequences.txt"
    response['Content-Disposition'] = 'attachment;filename='+download_file
    response['Content-Length'] = file.tell()
    return response

The template data

{% extends "database/base.html" %}
{% load static %}
{% block content %}
<!doctype html>
<div id="wrapper">
  <p> <b> Download the fasta sequences by categories </b> </p>
      {% for prefix in category_prefixes %}
            <a href="/download_category_{{ prefix|lower }}" >
              {{ prefix }}
        {% endfor %}

          <a href="/download_category_all" > All

{% endblock content %}

The url data

from django.urls import path
from database import views

path('download_data/', views.download_data, name='download_data'),
path('download_category_<str:category>', views.download_category,
  • \$\begingroup\$ It's fine you want a focus on this particular function, but I'm sure there's more to the script. Could you add that as well? \$\endgroup\$
    – Mast
    Commented Jan 7, 2020 at 6:52
  • 1
    \$\begingroup\$ I have added the script. Also here is the full database \$\endgroup\$
    – catuf
    Commented Jan 7, 2020 at 18:13
  • \$\begingroup\$ @catuf How is the program used, though? Also, I believe the indentation in your post is broken. \$\endgroup\$
    – AMC
    Commented Jan 8, 2020 at 22:58
  • \$\begingroup\$ I forgot to mention that anything more than minor optimizations will require other parts of the program. \$\endgroup\$
    – AMC
    Commented Jan 9, 2020 at 0:45
  • 1
    \$\begingroup\$ Can you add all your imports? \$\endgroup\$
    – Linny
    Commented Jan 9, 2020 at 19:13


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.