Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 485651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:25:24+00:00 2026-05-13T01:25:24+00:00

Django newbie question…. I’m trying to write a search form and maintain the state

  • 0

Django newbie question….

I’m trying to write a search form and maintain the state of the input box between the search request and the search results.

Here’s my form:

class SearchForm(forms.Form):
    q = forms.CharField(label='Search: ', max_length=50)

And here’s my views code:

def search(request, q=""):
    if (q != ""): 
        q = q.strip()
        form = SearchForm(initial=q) 
        #get results here...
        return render_to_response('things/search_results.html',
          {'things': things,  'form': form, 'query': q})
    elif (request.method == 'POST'): # If the form has been submitted
        form = SearchForm(request.POST) 
        if form.is_valid(): 
          q = form.cleaned_data['q']
          # Process the data in form.cleaned_data
          return HttpResponseRedirect('/things/search/%s/' % q) # Redirect after POST
        else:
          form = SearchForm() 
          return render_to_response('things/search.html', {
            'form': form,
          })
    else:
        form = SearchForm()
        return render_to_response('things/search.html', {
            'form': form,
        })

But this gives me the error:

Caught an exception while rendering: 'unicode' object has no attribute 'get'

How can I pass the initial value? Various things I’ve tried seem to interfere with the request.POST parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:25:24+00:00Added an answer on May 13, 2026 at 1:25 am

    Several things are not good here…

    1) The recommended thing after a POST is to redirect. This avoids the infamous popup saying that you are resubmitting the form when using the back button.

    2) You don’t need to say if request.method == 'POST', just if request.POST. That makes your code easier to read.

    3) The view generally looks something like:

    def myview(request):
        # Some set up operations
        if request.POST:
           form=MyForm(request.POST)
           if form.is_valid():
              # some other operations and model save if any
              # redirect to results page
        form=MyForm()
        #render your form template
    

    That is not to say that there can’t be much simpler and much more complicated views. But that is the gist of a view: if request is post process the form and redirect; if request is get render the form.

    I don’t know why you are getting an unicode error. I can only think that it is related to one of your models that you don’t provide.
    The error, as spookylukey mentions is in his comment, most likely is caused by you submitting a string instead of a dict to the initial parameter.

    I really recommend the django documentation, in particular the tutorial., but there is also the very nice Django Book.

    All that said, I think you want something like:

    def search(request, q=None):
        if request.POST:
            form = SearchForm(request.POST) 
            if form.is_valid(): 
               q = form.cleaned_data['q']
               url=reverse('search_results', args=(q,))
               return HttpResponseRedirect(url)
        if q is None:
            form = SearchForm() 
        else: 
            form = SearchForm(initial={'q': q})
        return render_to_response('things/search.html', {
            'form': form,
        })
    

    Notice that the parameter to initial is a dict of the field values of your form.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a Django newbie and am interesting in understanding how to install the EmailAuth
I'm a veteran Django programmer and a newbie in WordPress. In Django, we have
I am working on quick project , listing search results from bing and my
I have a form for address information. One of the fields is for the
I a new to django and python. I have however managed to build a
Django-admin is pluralizing a model that I have running as a proxy class. The
Django by default makes a primary key field on each model named id ,
Django, What's the best ,fastest way to get only first and last element from
Django's ORM (version 1.2.3) does not preserve identity when following foreign keys back and
I'm new to Google Apps Engine (working on an existing project for someone else)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.