0

I am trying to get a 'GET' parameter from one of my template pages, called 'id' (ex. issues.html?id=1), and using it to perform a variety of retrievals from my sqlite database. However, when I run the following code, I get the error "'QueryDict' object has no attribute 'id'"

Here is some of my code in views.py:

def issues(request):
    # Retrive basic legislation details from GET parameter
    legislation_id = request.GET.id
    legislation = Legislation.objects.filter(id = legislation_id)[0]

    # Getter functions from database - names are pretty self-explanatory
    def get_buttons():
        button_html = ""
        for tag in legislation.demographic_tags:
            button_html += "<input type='button' class='button' onclick='showtag"+tag.hash_code+"();' value='"+tag.name+"'>"
        return button_html
    def get_description():
        return legislation.description
    def get_script():
        return ""
    def get_sponsor():
        return legislation.sponsor
    def get_summary():
        return legislation.summary
    def get_title():
        return legislation.name

    # Return results back to page
    return render(request, 'issues.html',
{'buttons': get_buttons(), 'description': get_description(), 'script': get_script(), 'sponsor': get_sponsor(),
'summary': get_summary(), 'title': get_title()})

1 Answer 1

1

Your problem seems to be here: legislation_id = request.GET.id

request.GET is a dictionary so you need to get its key like this:

legislation_id = request.GET["id"]

You can also get the key with dict's get method as @Alvaro suggested: request.GET.get("id", 0). This will return 0 as default if id key ain't present and save you from KeyError exception.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

I would recommend using request.GET.get("id"). It will return None in case the dict doesn't have the key instead of raising KeyError (or even a default: request.GET.get("id", 0) )
@Alvaro you're right, although probably the whole view fails if the id is not there, your suggestion is very helpful. I'll include it.
@LordChiu consider accepting the answer if it was helpful so others can benefit from it too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.