我是Python / Django的新手并尝试创建搜索框,该搜索框从API中获取数据并在HTML上返回结果。就像任何其他搜索引擎一样。
我必须通过标头传递令牌。这是我目前的代码。
url.py
from django import forms
class SearchForm(forms.form):
search_company = forms.CharField(label='search_company',max_length=100)
form.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import SearchForm
def get_search (request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/search.html')
else:
form = SearchForm()
return render(request, "main/search.html")
views.py
<form class="col-md-6" action="/search.html/" method="post">
{% csrf_token %}
<label for="search_company">Search Company</label>
<input type="text" name="search_company">
<input type="submit" value="ok">
</form>
search.html
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f3cc120f0d0>
Traceback (most recent call last):
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/lucy/work/homebase/venv/HomeBase/local/lib/python3.5/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/home/lucy/work/homebase/venv/HomeBase/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/lucy/work/homebase/homebase/urls.py", line 18, in <module>
from metaphor import views
File "/home/lucy/work/homebase/metaphor/views.py", line 3, in <module>
from django import SearchForm
ImportError: cannot import name 'SearchForm'
这是我在控制台中的错误
<customErrors mode="On">
<error statusCode="404" redirect="~/Home/Error404" />
</customErrors>
修改:更新了新错误。
答案 0 :(得分:1)
来自django import SearchForm
ImportError:无法导入名称&#39; SearchForm&#39;
更新的错误是因为您尝试从django库的根目录导入SearchFrom。
您需要从应用中导入SearchForm
,所以在views.py
from django import SearchForm
应该是
from myapp.form import SearchForm
此外,在Django中,通常调用包含文件forms
(复数)而不是form
(单数)的表单,因为它可以包含多个表单。
答案 1 :(得分:0)
SearchForm位于您的django应用程序中,而不是您的django库中。
使用.form导入SearchForm
另外,将你的其他人移动到(如果是request.method == POST),这样如果方法没有发布,则发送表单。
我建议你使用reverse()函数来解析url,
HttpResponseRedirect(反向( '搜索'))