Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:24:25+00:00 2026-05-30T14:24:25+00:00

Thanks for helping. I have a view that allows me to upload a csv

  • 0

Thanks for helping.

I have a view that allows me to upload a csv file and to read it. I want this file’s data to be put in one of my template (and hence on a webpage). My view looks like this:

@login_required
def uploadFunc(request, username):
 user = get_object_or_404(User, username=username)
 if request.method == 'GET':
    return render_to_response('upload.html',{'user':user},context_instance=RequestContext(request))
 elif request.method == 'POST':
    with open('penguins.csv', 'rb') as f:
        reader = csv.reader(f)
        for m in reader:
            print m
        return HttpResponseRedirect("/")

As you can see from

print m
return HttpResponseRedirect("/")

I print the data extracted (it gets printed to terminal, but not a webpage), and then redirect the upload page to my home page where a table out of the penguin.csv (don’t laugh at the name! I know:)) should be constructed. Thing is I do not understand (or know) how do I let my template index.html to know that it is that file that should be put in that table.

Sorry for a lot of talk, if it is confusing feel free to ask questions.

Thanks again,
blargie-bla

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:24:27+00:00Added an answer on May 30, 2026 at 2:24 pm

    You need to first upload the file and save it into disk (or memory), then you need to save the this state for the next request. You have a few methods of doing that, for example in the session.

    Here’s an example, it’s not tested and might have some errors…

    FILE_UPLOAD_DIR = '/tmp'
    
    class UploadFileForm(forms.Form):
        file = forms.FileField()
    
    @login_required
    def uploadFunc(request, username):
        user = get_object_or_404(User, username=username)
        if request.method == 'GET':
            return render_to_response('upload.html',{'user':user},context_instance=RequestContext(request))
        elif request.method == 'POST':
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                path = _handle_uploaded_file(request.FILES['file'])
                request.session['uploaded_file'] = path
                return HttpResponseRedirect("/")
    
    def index(request):
        if request.session.get("uploaded_file", None):
            lines = []
            with open(request.session.pop("uploaded_file"), 'rb') as f:
                reader = csv.reader(f)
                for m in reader:
                    lines.append(m)
    
            return render_to_response('index.html', { 'file_lines': lines }, context_instance=RequestContext(request))
    
    
    def _handle_uploaded_file(source):
        fd, filepath = tempfile.mkstemp(prefix=source.name, dir=FILE_UPLOAD_DIR)
        with open(filepath, 'wb') as dest:
            shutil.copyfileobj(source, dest)
        return filepath
    

    Then in the template for index.html you can use the file_lines to populate the table.

    I “glued” this code from various snippets I found just now, here’s the list of urls:

    • https://docs.djangoproject.com/en/dev/topics/http/file-uploads/
    • Django File Upload
    • Need a minimal Django file upload example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for helping with this. I have a script that checks logins and then
Thanks to everyone out there helping newbies like me. So far I have this:
I have a zoo with daily data that looks like this: > head(almorol) 1973-10-02
I have a tab bar application, in one tab I have a view that
Thanks for helping me answering this question, I want to update the user's location
Sorry if this is too simple, but thanks in advance for helping. This is
Thank you all for helping. Below this post I put the corrected version's of
Thanks for reading this. I am dynamically generating some data which includes a select
[Django 1.0.2] I have a view set up like this: (r'^redirect/(?P<object_id>\d+)/(?P<url>.*)/$', 'django.views.generic.simple.redirect_to', {'content_type': SiteType},
I have a component that extends UIComponent of Flex, I want to add it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.