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 8462255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:06:59+00:00 2026-06-10T14:06:59+00:00

I have an application where I allow importing from CSV files. A list of

  • 0

I have an application where I allow importing from CSV files. A list of Libraries are contained in the CSV file. I need to first parse them into a list of Library instances, display them on another page, after user clicks “confirm”, these libraries are saved.

Here is my Form:

class LibCsvForm(forms.Form):
    lib_csv  = forms.FileField(label='CSV file')

class LibListForm(forms.Form):
    def __init__(self, *args, **kwargs):
        new_libs = kwargs.pop('new_libs')
        super(LibListForm, self).__init__(*args, **kwargs)
        self.fields['new_libs'] = new_libs

And my views:

def import_lib_csv(request):
    if request.method == 'POST':
        form = LibCsvForm(request.POST, request.FILES)
        if form.is_valid():
            raw = request.FILES['lib_csv'].read()
            new_libs = []
            # Process string 'raw' and add instances of Library to new_libs
            context = RequestContext(request, {'form': LibListForm(new_libs=new_libs)})
            context.update(csrf(request))
            return render_to_response('master/library.csv.confirm.html', context)
    else:
        form = LibCsvForm()
    variables = RequestContext(request, { 'form': form })
    return render_to_response('master/library.csv.html', variables)

A LibCsvForm is just for user to upload a file. Method “import_lib_csv” is to read the file, create instances, and pass them to another form LibListForm, which contains only ‘new_libs’. After the user clicks ‘confirm’, I would like to get this list and save the objects.

The problem is that, in the template:

{{ form.new_libs }}
{% for lib in new_libs %}
     {{ lib.id }}
{% endfor %}

It does not show anything. I think I did not pass the ‘new_libs’ into the form properly. Could anybody help? Thanks.


Edit:

Thanks, Rohan. I modified it as you mention. Now the library list can be displayed on in the template. ‘new_libs’ is a list of Library instances.

But when I click “confirm”, it reports error. Here is my method to save:

def save_lib_list(request):
    if request.POST:
        form = LibListForm(request.POST)
        print form.cleaned_data['new_libs']
    variables = RequestContext(request, {
        'lib_list': Library.objects.all()
    })
    return render_to_response('master/library.list.html', variables) 

Django says

"Exception Type:    KeyError
Exception Value:    'new_libs' "

My template is:

<form method="post" action="/library/savelist/" class="well">
    {% csrf_token %}
    <legend>Libraries to import</legend>
    {% for lib in new_libs %}
        {{ lib.id }}
    {% endfor %}
</form>

I think i miss something here, but not know how to solve…


Solved:

Save the content of uploaded CSV file on the server first. Set the path of this file as a hidden field in the LibListForm:

tmp_lib_file = forms.CharField(widget=forms.HiddenInput(), initial='tmp.lib')

Then when user clicks ‘confirm’, parse the file again and get instances of Library, save them.

  • 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-06-10T14:07:00+00:00Added an answer on June 10, 2026 at 2:07 pm

    You should add the new_libs in the template context if you want to use it in template.

    context = RequestContext(request, {'form': LibListForm(new_libs=new_libs), 'new_libs': new_libs})
    

    Also, what is the type of new_libs that you pass to LibListForm? If its not a proper field type if would not be displayed/processed properly.

    Update after question edit:

    • The error suggests that there is no new_fields key in the form.cleaned_data. The form should have a field with that name. new_field doesn’t seem to be a form field.
    • Also your template doesn’t have form fields?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an application which we need to allow users from our customer's systems
I have an application that allow users to choose picture from native gallery then
I have an application where I allow users to upload files, mainly PDF and
I have an application that imports data read from text files from a directory
I have an application that let users export files from a custom template filled
i have an application where we allow users to use Oauth2 for authentication and
I have an mvc application that I allow businesses to log in using https://storea.mydomain.com
I have modified the Nerd Dinner application to allow editing of child records by
I have database application, I want to allow the user to restore the deleted
In my application I have large area (≈5000x5000pts) and I must allow user to

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.