django - 在视图中创建函数。“分配前参考”
问题描述
我正在尝试创建一个简单的函数来接收从先前视图传递的请求数据。我想使用数据来创建示例中的条件(注释代码)。那么我创建:
def get_request(field_name):
data = request.GET.get('%s'%(field_name), False)
return data
在我的views.py中我使用:
from .helper import get_request
def search_simple(request):
#brand=data from previous view
get_request(brand) #this raises an exception
request.GET.get('brand', False) #it work good
# and them i wont using somethin like this
#if get_request(brand):
# do something
return render(request, 'search/search_results_brand.html')
这会引发以下异常:
local variable 'brand' referenced before assignment
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/wyniki-wyszukiwania/?brand=2&model=&province=&type_car=&transmission=&price_st=&price_end=&year_production_st=&year_production_end=&car_mileage=60do100&fuel_type=BenzynaLPG&engine_power=120-150&condition=Uszkodzony&automatic_air_conditioning=on&speed_limit=on&parking_heating=on&light_xenon=on&automatic_air_conditioning=on&speed_limit=on&parking_heating=on&light_xenon=on&search=
Django Version: 2.2.4
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'app',
'app_profile',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'crispy_forms',
'django_select2',
'widget_tweaks']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\tymot\Desktop\car-app\app_rama\app\views.py" in search_simple
36. get_request(brand)
Exception Type: UnboundLocalError at /wyniki-wyszukiwania/
Exception Value: local variable 'brand' referenced before assignment
如何使用我的功能正确获得品牌。品牌并不总是传递下去。
解决方案
问题是当你应该传递一个字符串时,Python 认为你正在将一个变量传递给你的函数。尝试替换get_request(brand)
为get_request('brand')
.
推荐阅读
- html - 显示:块不适用于图像以消除间隙
- ios - 如何通过 Swift 的 API 将自定义贴纸从我的应用程序共享到电报应用程序?
- derivative - 在 OpenMDAO 的 ExecComp 中,shape_by_conn 是否与 has_diag_partials 兼容?
- authentication - cors 错误预检导致 apollo 客户端和服务器的来源不匹配
- node.js - Mongoose 子文档更新:点符号赋值或 .findByIdAndUpdate()
- python - 使用 FiPy 求解一维球体中的菲克第二扩散定律
- configuration - 更改 docusaurus 中的导航标题链接
- asp.net - 为什么服务器显示旧的 ASPX 页面(即使已删除)?
- javascript - 如何在 Django 中将变量(列表)传递给 JavaScript?
- python - 在python中将轮廓点转换为边界框