django query : 변수 대 하드 코드 값으로 조회
-
19-09-2019 - |
문제
객체를 쿼리하려고 노력하고 있으며 값을 하드 코딩 할 때는 작동하지만 변수를 사용하면 쿼리가 작동하지 않습니다.
수업은 다음과 같습니다.
class AdvertisementType(models.Model):
type = models.CharField(max_length='40')
description = models.CharField(max_length='80')
def __unicode__(self):
return '%s' % self.type
다음은 다음과 같습니다.
self.type_ad = AdvertisementType.objects.get(type=type_of_ad)
예를 들어, type = "내부 페이지"인 광고 유형이 있습니다.
이 문장을 사용할 때 :
self.type_ad = AdvertisementType.objects.get(type="Inner Page")
모든 것이 잘 작동하지만 내가한다면
self.type_ad = AdvertisementType.objects.get(type=type_of_ad)
오류가 발생합니다
Caught an exception while rendering: AdvertisementType matching query does not exist.
type_of_ad = "내부 페이지"인 경우에도
어떤 아이디어?
해결책
나는 그것을 알아. 문제는 내가 전화했을 때입니다
tag_name, number, type_ad = token.split_contents()
The Type_ad가 템플릿 태그에서 ""를 얻는 것을 잊었습니다.
내가 쫓을 때
self.type_ad = AdvertisementType.objects.get(type=type_of_ad)
에게
self.type_ad = AdvertisementType.objects.get(type=type_of_ad[1:-1])
모든 것이 효과가있었습니다
제휴하지 않습니다 StackOverflow