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 556579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:56:05+00:00 2026-05-13T11:56:05+00:00

I have this view function: def search(request): if request.method == ‘GET’: form = SearchForm(request.GET)

  • 0

I have this view function:

def search(request):
    if request.method == 'GET':
        form = SearchForm(request.GET)
        if form.is_valid():
            last_name = form.cleaned_data['last_name']
            first_name = form.cleaned_data['first_name']
            lawyers = Lawyer.objects.all()
            [ other if statements ]
            ....
    else:
        form = SearchForm()
        return render_to_response('search.html', {'form': form})

I would think that when the page loads the else statement will be executed with the initial blank form. But this is not the case. To have the form displayed initially I have to add it inside the first if:

def search(request):
    if request.method == 'GET':
        form = SearchForm(request.GET)
        if form.is_valid():
            last_name = form.cleaned_data['last_name']
            first_name = form.cleaned_data['first_name']
            lawyers = Lawyer.objects.all()
            [ other if statements ]
            ....
        form = SearchForm()
        return render_to_response('search.html', {'form': form})
    else:
        form = SearchForm()
        return render_to_response('search.html', {'form': form})

Can you help why the first if is never false? Thank you.

The entire view function is here

  • 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-13T11:56:05+00:00Added an answer on May 13, 2026 at 11:56 am

    When you post a form with method=GET you don’t actually change the method from the initial request: it was method=GET too.

    You can either use method=POST in your form, and your if statement will check for that, or you could check for the existence of the required fields, first_name, last_name or both, as your form requires.

    if 'first_name' in request.GET or 'last_name' in request.GET:
        form = SearchForm(request.GET)
    else:
        form = SearchForm()
    

    You might want to abstract this into the form itself, modifying the __init__ method, but you don’t have to.

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

Sidebar

Related Questions

Heyy there, i have for example a view function like this: def profile_view(request, id):
This view function: @login_required def dashboard(request): from myproject.myapp.models import UserProfile k = UserProfile.objects.get( user=request.user.pk
I have the following logic in my view: def view_function(request): if request.method == 'POST':
I have code like this in my view model: function ChatListViewModel(chats) { var self
In the main view i have this function public function lang($file, $language){ require 'languages/'.$language.'/'.
I have this Javascript: <script language=javascript type=text/javascript> $().ready(function() { $('#ex2').jqm({ajax: 'view.php?id=<?=$objResult[id];?>', trigger: 'a.ex2trigger'}); });
I have grid something like this: Ext.define('Exp.view.dashboard.Tv', { extend: 'Ext.grid.Panel', initComponent: function() { this.columns
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have this following view which I get data from a model and thereafter
I have this function in module MessagesHelper : def message_block( message, ticket, comment )

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.