django q 对象条件匹配

发布于 2024-11-04 17:24:52 字数 1013 浏览 0 评论 0原文

我的问题是,当我选择 datefrom 和 dateto 时,它显示错误。“在中找不到关键‘性别’

现在即使我使用 Q 对象仍然存在与之前相同的问题..

search_gender = request.POST["gender"]
age_from = request.POST["age_from"]
age_to = request.POST["age_to"]
date_from = request.POST["date_from"]
date_to = request.POST["date_to"]

if date_from:
    date_from = datetime.strptime(request.POST["date_from"],"%d %B %Y")

if date_to:
    date_to = datetime.strptime(request.POST["date_to"],"%d %B %Y")

patient = PatientInfo()

p_male = 0
p_female = 0
age1 = 0
age2 = 0
date1 = 0
date2 = 0

if search_gender == 'male' :
    p_male = 1
if search_gender == 'female' :
    p_female = 1

if age_from:
    age1 = 1
if age_to:
    age2 = 1
if date_from:
    date1 = 1
if date_to:
    date2 = 1

if date1 and date2:
    patient = PatientInfo.objects.filter(
            Q(dateedit__range = (date_from,date_to))
            )

当我选择 date_from 和 date_to 两者时仍然显示错误???即使我使用了 if request.POST.has_key('gender') 仍然错误,我也没有选择男性或女性 我是 django 的新手..请帮忙...提前感谢..

my question is when i select datefrom and dateto it showing error ."Key 'gender' not found in

now even i used the Q object still same problem as earlier..

search_gender = request.POST["gender"]
age_from = request.POST["age_from"]
age_to = request.POST["age_to"]
date_from = request.POST["date_from"]
date_to = request.POST["date_to"]

if date_from:
    date_from = datetime.strptime(request.POST["date_from"],"%d %B %Y")

if date_to:
    date_to = datetime.strptime(request.POST["date_to"],"%d %B %Y")

patient = PatientInfo()

p_male = 0
p_female = 0
age1 = 0
age2 = 0
date1 = 0
date2 = 0

if search_gender == 'male' :
    p_male = 1
if search_gender == 'female' :
    p_female = 1

if age_from:
    age1 = 1
if age_to:
    age2 = 1
if date_from:
    date1 = 1
if date_to:
    date2 = 1

if date1 and date2:
    patient = PatientInfo.objects.filter(
            Q(dateedit__range = (date_from,date_to))
            )

still showing error when I select date_from and date_to both ??? not getting exactly even I neither select male nor female even I used if request.POST.has_key('gender') still error
and I am new to django.. plz help... thanx in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-11-11 17:24:53

当尝试查找字典中不存在的键时,会引发您报告的错误 - 几乎正是错误告诉您的内容。

由于我所要做的就是您发布的代码,我的猜测是,无论出于何种原因,“性别”实际上并不存在于 request.POST 字典中。你需要找出原因。

一般来说,在处理字典时,最佳实践是首先检查键是否存在或使用 dict.get 方法提供后备:

# Check for dictionary key
if request.POST.has_key('gender'):
    gender = request.POST['gender']

# or Provide fallback
gender = request.POST.get('gender', 'male') # if gender isn't present, 'male' will be returned

The error you reported is raised when a dictionary lookup is attempted for a key that doesn't exist in that dictionary -- pretty much exactly what the error is telling you.

Since all I have to go on is the code you posted, my guess would be that for whatever reason "gender" is not actually present in the request.POST dictionary. You need to find out why.

In general, when dealing with dictionaries, the best practice is to either check for the presence of the key first or use the dict.get method to provide a fallback:

# Check for dictionary key
if request.POST.has_key('gender'):
    gender = request.POST['gender']

# or Provide fallback
gender = request.POST.get('gender', 'male') # if gender isn't present, 'male' will be returned
冷︶言冷语的世界 2024-11-11 17:24:53

为此,表格将非常有用!

类似于:

forms.pyviews.py

from django import forms

class MyForm(forms.Form):
    GENDERS = (
        ('m','Male'),
        ('f', 'Female')
    )
    gender    = forms.CharField(choices=GENDERS, required=False)
    age_from  = forms.IntegerField()
    age_to    = forms.IntegerField()
    date_from = request.POST["date_from"]
    date_to   = forms.DateTimeField()


    def clean_gender(self):
        gender = self.cleanded_data.get('gender', 'm')

        #do something with gender

        return gender#now it has always a value

def myform(request):

if request.method == "POST":
    form = MyForm(request.POST)

    if form.is_valid():
        gender = form.cleaned_data.get('gender')

        #do something with data...


else:
    form = MyForm()

return render_to_response('template.html', {'form':from})

For this a form would be very usefull!

Something like:

forms.py

from django import forms

class MyForm(forms.Form):
    GENDERS = (
        ('m','Male'),
        ('f', 'Female')
    )
    gender    = forms.CharField(choices=GENDERS, required=False)
    age_from  = forms.IntegerField()
    age_to    = forms.IntegerField()
    date_from = request.POST["date_from"]
    date_to   = forms.DateTimeField()


    def clean_gender(self):
        gender = self.cleanded_data.get('gender', 'm')

        #do something with gender

        return gender#now it has always a value

views.py:

def myform(request):

if request.method == "POST":
    form = MyForm(request.POST)

    if form.is_valid():
        gender = form.cleaned_data.get('gender')

        #do something with data...


else:
    form = MyForm()

return render_to_response('template.html', {'form':from})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文