Robus
Robus

Reputation: 465

Passing form input data to post request

I'm very new to Python/Django and trying to create search box which grabs the data from an API and returns the result on HTML. Just like any other search engine does.

I have to passed the token via headers. This is my current code.

url.py

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$',views.search, name='search'),]

form.py

from django import forms

class SearchForm(forms.form):
search_company = forms.CharField(label='search_company',max_length=100)

views.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")

search.html

  <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>

this is my error in console

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'

edit: Updated post with new error.

Upvotes: 0

Views: 125

Answers (2)

Luv33preet
Luv33preet

Reputation: 1867

The SearchForm is in your django app and not in your django library.

Use from .form import SearchForm

Also, move your else to ( if request.method == POST ), so that the form is sent if method is not post.

And I would suggest you to use reverse() function to resolve url ,

HttpResponseRedirect(reverse('search'))

Upvotes: 0

Nrzonline
Nrzonline

Reputation: 1618

from django import SearchForm
ImportError: cannot import name 'SearchForm'

The updated error is because you are trying to import the SearchFrom from the root of the django library.

You need to import your SearchForm from your app, so in your views.py

from django import SearchForm

should be something like

from myapp.form import SearchForm

Also, in Django it is common to call your forms containing file forms(plural) instead of form (singular), since it can contain multiple forms.

Upvotes: 1

Related Questions