'SearchForm' 对象没有属性 'get'
'SearchForm' object has no attribute 'get'
我正在尝试使用 DateField 创建 SearchForm,但是当我发送数据 method="post" 时,表单看不到属性 'get'。错误在哪里?
forms.py
class SearchForm(forms.Form):
datee = forms.DateField(input_formats=['%Y-%m-%d'],
widget=forms.widgets.DateInput(format="%Y-%m-%d"))
views.py
def index(request):
search_form = search(request)
context = {'search_form': search_form}
return render(request, 'name/index.html', context)
def search(request):
if request.method == 'POST':
form = SearchForm(data=request.POST)
if form.is_valid():
#Do something for examlpe
HttpResponseRedirect(reverse("name:second"))
else:
form = SearchForm()
search_form = form
return search_form
index.html
<form method="post" action="{% url 'name:search' %}">
{% csrf_token %}
{{ search_form.as_p }}
<button name="submit">Search</button>
</form>
但我得到这个 log 并且不明白错误在哪里:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/search_result
Django Version: 2.0.5
Python Version: 3.6.3
Traceback:
File "C:\Users\Александр\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\Александр\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\deprecation.py" in __call__
97. response = self.process_response(request, response)
File "C:\Users\Александр\AppData\Local\Programs\Python\Python36\lib\site-packages\django\middleware\clickjacking.py" in process_response
26. if response.get('X-Frame-Options') is not None:
Exception Type: AttributeError at /search_result
Exception Value: 'SearchForm' object has no attribute 'get'
Django 视图应该 return httpresponse 对象。但是您的搜索视图 return 表单对象代替了。您可以将 serch 视图重写为类似这样的内容以修复错误:
def search(request):
if request.method == 'POST':
form = SearchForm(data=request.POST)
if form.is_valid():
#Do something for examlpe
HttpResponseRedirect(reverse("name:second"))
else:
form = SearchForm()
context = {}
context['search_form'] = form
return render(request, 'name/index.html', context)
Form
不应具有 get
、post
或其他 dispatch
ed HTTP 动词匹配方法,因为它用于表示数据并对其进行操作(更具体地说,方便地将任何有意义的操作——即除验证和清理之外的任何操作——委托给底层基础设施),不满足 HTTP 请求——后者是视图的责任。
你的 search
视图 return 是一个 SearchForm
实例,而它必须 return 一个 HttpResponse
兼容的对象,这就是错误的来源来自(注意 if response.get('X-Frame-Options') is not None:
)。
要解决此问题,请确保从 search
视图 return render(request, 'name/index.html', {'form': search_form})
。
我正在尝试使用 DateField 创建 SearchForm,但是当我发送数据 method="post" 时,表单看不到属性 'get'。错误在哪里?
forms.py
class SearchForm(forms.Form):
datee = forms.DateField(input_formats=['%Y-%m-%d'],
widget=forms.widgets.DateInput(format="%Y-%m-%d"))
views.py
def index(request):
search_form = search(request)
context = {'search_form': search_form}
return render(request, 'name/index.html', context)
def search(request):
if request.method == 'POST':
form = SearchForm(data=request.POST)
if form.is_valid():
#Do something for examlpe
HttpResponseRedirect(reverse("name:second"))
else:
form = SearchForm()
search_form = form
return search_form
index.html
<form method="post" action="{% url 'name:search' %}">
{% csrf_token %}
{{ search_form.as_p }}
<button name="submit">Search</button>
</form>
但我得到这个 log 并且不明白错误在哪里:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/search_result
Django Version: 2.0.5
Python Version: 3.6.3
Traceback:
File "C:\Users\Александр\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\Александр\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\deprecation.py" in __call__
97. response = self.process_response(request, response)
File "C:\Users\Александр\AppData\Local\Programs\Python\Python36\lib\site-packages\django\middleware\clickjacking.py" in process_response
26. if response.get('X-Frame-Options') is not None:
Exception Type: AttributeError at /search_result
Exception Value: 'SearchForm' object has no attribute 'get'
Django 视图应该 return httpresponse 对象。但是您的搜索视图 return 表单对象代替了。您可以将 serch 视图重写为类似这样的内容以修复错误:
def search(request):
if request.method == 'POST':
form = SearchForm(data=request.POST)
if form.is_valid():
#Do something for examlpe
HttpResponseRedirect(reverse("name:second"))
else:
form = SearchForm()
context = {}
context['search_form'] = form
return render(request, 'name/index.html', context)
Form
不应具有 get
、post
或其他 dispatch
ed HTTP 动词匹配方法,因为它用于表示数据并对其进行操作(更具体地说,方便地将任何有意义的操作——即除验证和清理之外的任何操作——委托给底层基础设施),不满足 HTTP 请求——后者是视图的责任。
你的 search
视图 return 是一个 SearchForm
实例,而它必须 return 一个 HttpResponse
兼容的对象,这就是错误的来源来自(注意 if response.get('X-Frame-Options') is not None:
)。
要解决此问题,请确保从 search
视图 return render(request, 'name/index.html', {'form': search_form})
。