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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:47:14+00:00 2026-05-30T19:47:14+00:00

I have the following logic in my view: def view_function(request): if request.method == ‘POST’:

  • 0

I have the following logic in my view:

def view_function(request):
    if request.method == 'POST':
        uploadform = UploadFileForm(request.POST, request.FILES)
        if uploadform.is_valid():
            #do stuff

Where UploadFileForm equals to:

class UploadFileForm(forms.Form):
    file = forms.FileField()

I am trying to write unit tests for this view. Looking in Django docs, the suggested way is this:

class test_stuffTest(TestCase):
    def setUp(self):
        self.client = django.test.client.Client()
    ...
    def test_stuff(self):
        myfile = open('....\file.csv','r')
        response = self.client.post('/', {'name':'file.csv','attachment':myfile})
        #check response

My goal is to get uploadform.is_valid() to evaluate to True, so I can test the code which follows the form validation. When I run the test above, uploadform.is_valid() evaluates to False. Is there anything I am missing? Is the code in my test adding the file to request.FILES, or is it doing something else?

  • 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-30T19:47:15+00:00Added an answer on May 30, 2026 at 7:47 pm

    In the docs, the file field is called attachment, but in yours, it’s called file.

    You don’t need name in your post data either — that refers to another field called name, not the name of the file that you are uploading.

    Try the following:

    def test_stuff(self): 
        myfile = open('....\file.csv','r') 
        response = self.client.post('/', {'file':myfile})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
The following in part of my view file def user_detail(request, username, template_name='auth/user_detail.html', extra_context=None): context
I need syntax help with the following code logic: I have a code block
In views.py I have the following view which gets called when a new user
I have the following view within an app called 'manager': class AddObj(CreateView): model =
suppose that you have a webpage that uses the post and get method, and
I have following problem: have to model classes (posts and users). Main view is
I'm using MVC3 and in my view I have the following code block; Response.Redirect(@Url.Action(index,home,
Given that we have the following View: <StackPanel> <TextBlock Text=Some text/> <Image Source={Binding vmImageProp}
I have the following 2 tables defined in migrations class CreateUsers < ActiveRecord::Migration def

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.