Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been asked to convert a Python application into a Django one but I'm totally new to Django.

I have the following problem, when I upload a file text that must be read to save its content into a database I find that Django is striping the "extra" whitespaces and I must keep those whitespaces.

This is my template

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>

<body>
    {% if newdoc %}
        <ul>
        {% for line in newdoc %}
            <li>{{ line }} </li>
        {% endfor %}
        </ul>
    {% endif %}

    <form action="{% url 'exam:upload' %}" method="post" enctype="multipart/form-data" content-type="text/plain">
        {% csrf_token %}
            <p>{{ form.non_field_errors }}</p>
            <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
            <p>
                    {{ form.docfile.errors }}
                    {{ form.docfile }}
            </p>
        <p><input type="submit" value="Upload" /></p>
    </form>
</body>

This is what I have in the views.py

def upload(request):

    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = request.FILES['docfile']
            form = DocumentForm()
            return render(request, 'exam/upload.html', {'newdoc': newdoc, 'form': form})
    else:
        form = DocumentForm() # A empty, unbound form

    return render(request, 'exam/upload.html', {
        'form': form,
    })

And this is my forms.py:

from django import forms

class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file',
        help_text='max. 42 megabytes'
)

Now when I upload the file, it shows a random line like this:

"09000021009296401 02 b a b a b b b d b b d d a +8589 +03+6942 +03+1461 +00+5093 +00+2 +00+9237 +01+60 +01+00 +00"

While it should be this:

"09000021009296401 02 b   a          b   a     b    b    b d  b    b      d    d                a                        +8589  +03+6942  +03+1461  +00+5093  +00+2     +00+9237  +01+60    +01+00    +00                    "

I must keep the extra spaces and they save this information into a database, which I cannot correctly do if I don't have all the spaces that the file has.

Also, before you ask, It is not related with the print format of Django, since in a previous test I already tryed to save the information into the model, but it has the same problem with spaces.

Thanks everyone.

share|improve this question

1 Answer

Change the template as follow:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>

<body>
    {% if newdoc %}
    <pre><code>{% for line in newdoc %}{{ line|safe }}{% endfor %}</code></pre>
    {% endif %}

    <form action="{% url 'exam:upload' %}" method="post" enctype="multipart/form-data" content-type="text/plain">
        {% csrf_token %}
            <p>{{ form.non_field_errors }}</p>
            <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
            <p>
                    {{ form.docfile.errors }}
                    {{ form.docfile }}
            </p>
        <p><input type="submit" value="Upload" /></p>
    </form>
</body>
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.