为什么'&'将Django CharField(python字符串)分成几部分?

时间:2012-03-12 05:40:03

标签: django-views

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

当用户从下拉列表中选择一个标题时,将调用此函数并执行该任务。 现在我输入了一个包含'&'的字符串&符号认为它可以像普通英语一样扮演'和'的角色: -

'CI-2-UGI&肠症状筛查&表征'(它是下拉值之一) 现在当用户从下拉列表中选择此值时,标题不会保持不变,而是标题更改为CI-2-UGI(在title = request.GET ['title']中)并且在函数执行之前我收到500错误页面。 这是错误在runserver模式下打印的内容

  

/home/user/cpms/careprep/tags/4.0/careprep/export/views.py(66)get_interview_type() - >无    - >通过   (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

此外,当我从标题中删除“&”&符号时,没有500页。 为什么这个&造成这个问题?我认为通过这种方式,每个特殊符号都会导致同样的问题。如果有人可以指导我以及如何解决这个问题,我将不胜感激

2 个答案:

答案 0 :(得分:1)

因为&用于分隔查询字符串中的字段。首先对URL进行编码。

答案 1 :(得分:0)

I studied about URL-encode what Ignacio suggest but i found a clean and better solution.
Instead of comparing with title i used 'id' <option value="">  which is the primary key(id) for
Interview Table and modified the code like this:

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


and for picking the id i use jquery:-
$(function(){   
    $("#id_title").bind("change",function(){
        id = $(this).find("option[value="+ $(this).val()+"]").val();
        url = "../get_interview_type/?id="+id;
        $.get(url, "", function (d){
            $("#interview_dd").html(d);
        })
    });
});