Why '&' is breaking Django CharField(python strings) into parts?

266 views
Skip to first unread message

Nikhil Verma

unread,
Mar 12, 2012, 4:18:38 AM3/12/12
to django...@googlegroups.com
    I have a function in views.py
   
        def get_interview_type(request):
            i = None
            title = request.GET['title'] #Here the title becomes different
            try:
                i = Interview.objects.get(title=title) #it looks for that dropdown value
                #Error according to pdb
                #-> i = Interview.objects.get(title=title)
                #    (Pdb)
                #    DoesNotExist: DoesNotE...exist.',)
   
                if i.interview_type == "Time Series":
                    visit_ids = i.visit_set.all()
                    reference_visit_list = []
                    for visit in visit_ids:
                        reference_visit_list.append(visit.reference_visit)
                    reference_visit_list.extend(visit_ids)
                    list(set(reference_visit_list))
                    len_visits=filter(None,reference_visit_list)
                    total_visits = len(len_visits)
                    return render_to_response('export/get_details.html',
                                          {'visits':visit_ids,'count':visit_ids.count(),
                                           'total_visits':total_visits},
                                           context_instance=RequestContext(request)
                                          )
                else:
                    return render_to_response('export/get_interview_type.html',
                                          {'visits':i.visit_set.all()},
                                           context_instance=RequestContext(request)
                                        )
            except Interview.DoesNotExist:
                pass
       
When the user selects a title from the dropdown this function is called and does it tasks.
Now i have entered a string which include '&' ampersand thinking that it can play a role of 'and' in normal english like this :-

'CI-2-UGI & Bowel Symptom Screening & Characterization'(It is one of that dropdown value)
Now when user selects this value from dropdown the title does not remain the same, instead the title changes to  CI-2-UGI(in title = request.GET['title']) and  before the function executes i recieve a 500 error page.
This is what the error prints in runserver mode
> /home/user/cpms/careprep/tags/4.0/careprep/export/views.py(66)get_interview_type()->None
-> pass
(Pdb) c
[11/Mar/2012 22:05:20] "GET /export/get_interview_type/?title=CI-2-UGI%20&%20Bowel%20Symptom%20Screening%20&%20Characterization HTTP/1.1" 500 64490
       
Also when i remove that '&'ampersand from the title there is no 500 page.

Now & is breaks the quesrystring what i know then how to solve the problem. I try having a look to url -encode but no luck .

I want whatever the drop value contains (&,@ ....etc) it should not break ? How to solve the problem ?

Thanks in advance

--
Regards
Nikhil Verma
+91-958-273-3156

yati sagade

unread,
Mar 12, 2012, 10:23:55 AM3/12/12
to django...@googlegroups.com
In an HTTP request, the query parameters are separated by the ampersand. A form that has the fields, say, name and email, when submitted via GET, the query string will look like "name=myname&email=so...@email.com". Since your html is using literal '&', Django(and any other request parser) will think of it as the standard delimiter, and will typically split the query string on '&'. That is why your code is breaking.

There are multiple solutions. The simplest I can think of is to use "&" - without the quotes and WITH the semicolon - whenever you need an ampersand (&) in your HTML. That should fix this. There are other ways, like percentage encoding (http://www.blooberry.com/indexdot/html/topics/urlencoding.htm) you can use when you need to generate URLs that contain a literal ampersand.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Yati Sagade

(@yati_itay)


Nikhil Verma

unread,
Mar 12, 2012, 2:57:27 PM3/12/12
to django...@googlegroups.com
Thnaks yati i solved the problem by comparing it with id instead of title which is the primary key of that Interview Table.


def get_interview_type(request):
    i = None
    id = request.GET['id']
    try:
        i = Interview.objects.get(id=id)

        if i.interview_type == "Time Series":
            visit_ids = i.visit_set.all()
            reference_visit_list = []
            for visit in visit_ids:
                reference_visit_list.append(visit.reference_visit)
            reference_visit_list.extend(visit_ids)
            list(set(reference_visit_list))
            len_visits=filter(None,reference_visit_list)
            total_visits = len(len_visits)
            return render_to_response('export/get_details.html',
                                  {'visits':visit_ids,'count':visit_ids.count(),
                                   'total_visits':total_visits},
                                   context_instance=RequestContext(request)
                                  )
        else:
            return render_to_response('export/get_interview_type.html',
                                  {'visits':i.visit_set.all()},
                                   context_instance=RequestContext(request)
                                )
    except InterviewTitle.DoesNotExist:
        pass

Is this a good approach ?

Thanks
Regards
Nikhil Verma
+91-958-273-3156

ajohnston

unread,
Mar 12, 2012, 6:08:38 PM3/12/12
to django...@googlegroups.com

Is this a good approach ?



Yes, that is a better approach. I was going to suggest that earlier, but only now had time to post.

Nikhil Verma

unread,
Mar 12, 2012, 9:28:51 PM3/12/12
to django...@googlegroups.com
Thanks

On Tue, Mar 13, 2012 at 3:38 AM, ajohnston <ajohns...@gmail.com> wrote:

Is this a good approach ?



Yes, that is a better approach. I was going to suggest that earlier, but only now had time to post.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/t_bxzjIdep4J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages