Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试具有post请求的django视图_Django_Python 3.x - Fatal编程技术网

测试具有post请求的django视图

测试具有post请求的django视图,django,python-3.x,Django,Python 3.x,我试图测试我的django视图,其中包含post请求 def save_movie(request): ''' Fetch data and save to Movie model data obtained from post request is a string so need to clean it and convert to json ''' if request.method == 'POST': movie_data = str(request.POST['movie_

我试图测试我的django视图,其中包含post请求

def save_movie(request):
'''
Fetch data and save to Movie model 
data obtained from post request is a string so need to clean it and convert to json
'''
if request.method == 'POST':
    movie_data = str(request.POST['movie_data'])
    movie_data = movie_data.replace("\'","\"")
    b=""
    for i in range(len(movie_data)):
        if movie_data[i] is "\"" and movie_data[i-1].isalpha() and movie_data[i+1].isalpha():
            b+="\'"
        else:
            b+=movie_data[i]
    json_data = json.loads(b)
    new_movie, created = Movie.objects.get_or_create(title=json_data['title'], episode_id=int(json_data['episode_id']), opening_crawl=json_data['opening_crawl'], director=json_data['director'], producer=json_data['producer'], release_date=json_data['release_date'], characters=str(json_data['characters']), planets=str(json_data['planets']), starships=str(json_data['starships']), vehicles=str(json_data['vehicles']), species=str(json_data['species']), created=parse_datetime(json_data['created']), edited=parse_datetime(json_data['edited']), url=json_data['url'])
    if created:
        new_movie.save()
        return HttpResponse('<h1>Movie Saved</h1>')
    else:
        return HttpResponse('<h1>Movie already Saved</h1>')
当我运行这个测试用例时,我得到了多值错误

  File "/home/abhishek/Desktop/SpotDraft/StarWars/favourites/tests/test_views.py", line 58, in test_save_movie
    response = self.client.post(self.save_movie_url, {'title':'test', 'episode_id':1, 'opening_crawl':'test for movie', 'director':'director', 'producer':'producer', 'release_date':'test_date', 'characters':'test_chars', 'planets':'test_planets', 'starships':'test_starships', 'vehicles':'str_vehicles', 'species':'str_species', 'created':'2014-12-09T13:50:49.641000Z', 'edited':'2015-12-09T13:50:49.641000Z', 'url':'https://test.com'})
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 526, in post
    response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 356, in post
    secure=secure, **extra)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 421, in generic
    return self.request(**r)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 496, in request
    raise exc_value
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/abhishek/Desktop/SpotDraft/StarWars/favourites/views.py", line 109, in save_movie
    movie_data = str(request.POST['movie_data'])
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'movie_data'
它似乎找不到应该出现在我的post请求中的movie_数据,因为视图从那里提取了它
如何解决此问题?

您能否共享完整的测试,包括作为post数据发送的整个字典?1)您的视图似乎正在处理JSON,但正在接受正常的post数据并进行大量预处理?2) 您没有在POST数据中传递名为“movie_data”的键?编辑了问题并添加了完整的字典。您的视图需要POST值“movie_data”,从外观上看应该是JSON字符串?你的帖子应该是self.client.post(self.save\u movie\u url,json.dumps({…}))
  File "/home/abhishek/Desktop/SpotDraft/StarWars/favourites/tests/test_views.py", line 58, in test_save_movie
    response = self.client.post(self.save_movie_url, {'title':'test', 'episode_id':1, 'opening_crawl':'test for movie', 'director':'director', 'producer':'producer', 'release_date':'test_date', 'characters':'test_chars', 'planets':'test_planets', 'starships':'test_starships', 'vehicles':'str_vehicles', 'species':'str_species', 'created':'2014-12-09T13:50:49.641000Z', 'edited':'2015-12-09T13:50:49.641000Z', 'url':'https://test.com'})
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 526, in post
    response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 356, in post
    secure=secure, **extra)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 421, in generic
    return self.request(**r)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 496, in request
    raise exc_value
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/abhishek/Desktop/SpotDraft/StarWars/favourites/views.py", line 109, in save_movie
    movie_data = str(request.POST['movie_data'])
  File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'movie_data'