블로그 이미지
흰색앵초

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2013. 4. 3. 16:30 프로그래밍/장고

장고로 검색기능을 구현할 때 영어의 대소문자를 구분하지 않고 검색해야할 필요가 있다

그럴때는 contains대신 icontains를 사용해주면 된다.

test = Test_table.objects.filter(title__icontains='abcd')

위와같이 검색을 하게 되면 Abcd aBcd ... 등등등의 경우에 대소문자를 가리지 않고 검색이 된다.


posted by 흰색앵초
2013. 4. 1. 17:08 프로그래밍/장고

장고에서 단순히 Object의 갯수만을 세고 싶을때는 .count()를 사용하면 된다.


count

count()

Returns an integer representing the number of objects in the database matching the QuerySet. The count() method never raises exceptions.

Example:

# Returns the total number of entries in the database.
Entry.objects.count()

# Returns the number of entries whose headline contains 'Lennon'
Entry.objects.filter(headline__contains='Lennon').count()

posted by 흰색앵초
2013. 3. 22. 13:25 프로그래밍/파이썬

파이썬 스크립트를 만들어서 사용하다보면 불가피하게 중복실행을 방지해야할 필요가 있다. 그럴 때는 아래와 같은 코드를 이용하면 쉽게 피할 수 있다.


import fcntl
pid_file = 'program.pid'
fp = open(pid_file, 'w')

try:
    fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # another instance is running
    sys.exit(1)

스크립트가 실행되면 program.pid에 락을 걸어서 중복실행을 막는 구조로 되어있다. 이러한 경우 파일을 처음 만들어 쓴 유저로 권한이 가기때문에 권한 문제가 생길 수 있으니 주의해야한다.


출처 : http://stackoverflow.com/questions/220525/ensuring-a-single-instance-of-an-application-in-linux


posted by 흰색앵초
2013. 3. 5. 23:12 프로그래밍/기타등등

익스플로러를 끄고 첨부된 레지스트파일을 사용한 뒤 다시 켜본다.

윈도우7에서만 작동확인했으니 다른 운영체제에서는 되도록 사용을 안하는 것이 좋을지도 -_-;

ie.reg


posted by 흰색앵초
2013. 2. 26. 19:27 프로그래밍/장고
views.py에서 아래와 같이 사용하면 된다. 원하는대로 약간씩 고쳐서 쓰면 무방할 듯
def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

출처 : http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django


posted by 흰색앵초
2013. 2. 26. 19:14 프로그래밍/장고


DateTimeField에 서버의 시간을 넣고 싶을 때가 있다 그럴 때는 models.py에서 아래와 같이 넣어주면 된다.

date_modified = models.DateTimeField(auto_now=True)

혹은 다른 방법도 존재한다.


views.py 부분에서

from datetime import datetime
obj.date_modified = datetime.now()

로 처리해서 사용하면 된다.


출처:http://stackoverflow.com/questions/7465796/django-set-datetimefield-to-servers-current-time

posted by 흰색앵초

웹에디터인 CKEditor를 사용하게 되면 줄바꿈이 될때마다 <p>....</p>식으로 p태그가 자동으로 삽입됩니다. 

이러한 경우를 피하기 위해서는 아래와같이 설정하면 됩니다.

먼저 config.js를 열고

CKEDITOR.editorConfig = function( config ) {
	.....
	config.enterMode = CKEDITOR.ENTER_BR;
        .....

를 추가해주시면 됩니다.

posted by 흰색앵초

input 타입 중 checkbox는 다른 것과는 다르게 onclick이 작동하지 않는다.

이럴때는 아래와 같이 onclick대신 onchange를 사용하여서 처리할 수 있다.

<input type="checkbox" onchange="alert('ddd')" />

예제코드에서는 간단히 작동하는 것을 확인하기 위해 alert을 사용하였고 저자리에 자바스크립트 function을 넣어서 사용하면 간단히 활용할 수 있다.

posted by 흰색앵초

팝업으로 로그인 처리를 한 후 부모 페이지를 리프레쉬 해야할 경우가 있다 그럴 때는 아래와 같이 처리하면 된다.

부모창

<a href="#" onclick='window.open("팝업창 주소","_blank","height=500,width=500, status=yes,toolbar=no,menubar=no,location=no");return false'>로그인</a>


팝업 로그인 완료 페이지

<script language="javascript" type="text/javascript">
    setTimeout(function() {
    opener.location.reload(); //부모창 리프레쉬
    self.close(); //현재창 닫기
    }, 2000); // 2초후 실행 1000당 1초
</script>


팝업페이지 내에서 로그인이 완료된 페이지 부분에 자바스크립트를 추가해줌으로써 쉽게 처리할 수 있다.

posted by 흰색앵초
2012. 12. 22. 21:34 프로그래밍/장고

장고에서 media, static 폴더를 셋팅해야할일들이 많다. nginx나 apache를 붙여서 쓸 때는 따로 셋팅해야하지만, 개발용서버에서의 셋팅 방법은 settings.py에서 폴더를 셋팅한 다음 urls.py를 아래와 같이 추가하면 사용할 수 있다.

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )


posted by 흰색앵초
prev 1 2 3 4 next