How to Display .txt file in django with html and django pagination?


(Mit Patel) #1

I am using django 2.1.5 when ever i sent the pdf file in django pagination and display the pdf in browser its work fine but in case of .txt file it will be downloaded all file just path i sent because the file is generated dynamically

            {% if pages.has_other_pages %}
              <ul class="pagination">
                {% if pages.has_previous %}
                  <li><a href="?page={{ pages.previous_page_number }}">&laquo;</a></li>
                {% else %}
                  <li class="disabled"><span>&laquo;</span></li>
                {% endif %}
                {% for i in pages.paginator.page_range %}
                  {% if pages.number == i %}
                    <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                  {% else %}
                  <li><a href="?page={{ i }}">{{ i }}</a></li>
                  {% endif %}
                {% endfor %}
                {% if pages.has_next %}
                      <li><a href="?page={{ pages.next_page_number }}">&raquo;</a></li>
                {% else %}
                    <li class="disabled"><span>&raquo;</span></li>
              {% endif %}
            </ul>
            {% endif %}
          </div>
            {% endfor %}

but if i do same thing for txt file than it will download file


(Vitor Freitas) #2

Hi @Mit_Patel :smiley:

I’m not sure if I understood the question… are you trying to paginate a .txt file? Can you share a little bit of your code so we can understand it a little bit better?

If you want to return a plain text response with Django you can do something like:

def plain_text_view(request):
    return HttpResponse('Hello, World!', content_type='text/plain')

Or you could read it from a text file:

def plain_text_view(request):
    file = open('/path/to/myfile.txt', 'r')
    content = file.read()
    file.close()
    return HttpResponse(content, content_type='text/plain')

(Mit Patel) #3
            {% if pages.has_other_pages %}
              <ul class="pagination">
                {% if pages.has_previous %}
                  <li><a href="?page={{ pages.previous_page_number }}">&laquo;</a></li>
                {% else %}
                  <li class="disabled"><span>&laquo;</span></li>
                {% endif %}
                {% for i in pages.paginator.page_range %}
                  {% if pages.number == i %}
                    <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
                  {% else %}
                  <li><a href="?page={{ i }}">{{ i }}</a></li>
                  {% endif %}
                {% endfor %}
                {% if pages.has_next %}
                      <li><a href="?page={{ pages.next_page_number }}">&raquo;</a></li>
                {% else %}
                    <li class="disabled"><span>&raquo;</span></li>
              {% endif %}
            </ul>
            {% endif %}
          </div>
            {% endfor %}

like i have to compare two file on browser the one portion of screen is pdf and second portion of screen is .txt file of that pdf and user is allow to edit the .txt file in browser in django so can you please help me out !!! I’m Stuck here from last 2 days .