-
카테고리
-
세부 분야
풀스택
-
해결 여부
해결됨
(게시판 댓글구현 오류) count 부분에서 오류가 나는데.. 어떻게 해야할까요..ㅠ?
22.04.24 01:12 작성 조회수 437
2
안녕하세요
남박사님
남박사님 덕분에 강의를 잘듣고 진도를 나가고 있습니다. 이해가 안가는 부분도 아직 있지만 열심히 배울려고 노력하고 있는데요!
이번 댓글기능 만들기 부분에서 오류나는 부분있어서 질문올립니다.
댓글기능 구연하고 마지막 부분에서 jinja2.exceptions.UndefinedError: 'pymongo.cursor.Cursor object' has no attribute 'count'라고 오류가 나는데.. 저번에 {% if comments.count() > 0 %} 이부분이 몽고db 부분하고 연동이 안되는거 같아서 버전 차이라고 하셔서 count_documents({조건})로 변경해봤는데.. 오류는 나더라구요..!
- 오류 -
-코드 -
-board.py-
답변을 작성해보세요.
0
남박사
지식공유자2022.04.25
comment = mongo.db.comment
comments = comment.find({"root.idx": str(data.get("_id"))})
return render_template("view.html", result=result, comments=list(comments), page=page, search=search, keyword=keyword, title="글 상세보기")
제가 생각을 잘못했습니다. comments 를 넘길때 먼저 위 코드처럼 list로 형변환을 해서 넘기고,
{% if comments|length > 0 %}
HTML 의 jinja2 에서는 위 코드처럼 length 로 접근하게 수정해보시기 바랍니다.
문신호
질문자2022.04.25
남박사님 답변 주셔서 감사합니다!
오류났던 부분은 해결했는데요. view.html에 댓글이 생기지는 않는데.. 어떻게 해야할까요?
몽고DB에는 내용은 저장이 됩니다!
그리고 리스트에서 글작성을 쓴다음에 작성하기 누르기 전 summernote 에디터 창에서는 글작성이 정상적으로 동작되는데.. 저장하고 나면 글 작성내용이
하단부로 내려가던데 이건 왜그런걸까요?
- 이미지 - 1 -
- 이미지 - 2-
- 코드 -
- board.py -
남박사
지식공유자2022.04.25
comments = comment.find({"root.idx": str(data.get("_id"))})
print(comments)
먼저 파이썬 코드에서 위 코드처럼 comments 에 데이터가 정상적으로 확인해보시고,
{{comments|length}}
{{comments}}
{% if comments|length > 0 %}
{% for c in comments %}
HTML 에서도 위의 코드처럼 comments 의 갯수나 comments 내용을 화면에 출력해서 어떻게 나오는지 부터 확인해봐야 할듯 합니다. 이렇게 출력을 해보고 어느쪽에서 문제가 생기는건지 부터 알아야 어떻게 수정을 할지를 판단할 수 있습니다.
그리고 내용이 하단에 붙는이유는..
<td colspan="2"><div style="min-height: 200px;"></div>{% autoescape false %}{{result.contents}}{% endautoescape %}</td>
위 코드를 보면 <div> 태그가 열리고 바로 </div> 닫혀 있습니다. div 태그 사이에 내용이 출력되어야 합니다.
문신호
질문자2022.04.25
계속 물어봐서 죄송합니다~!
내용 확인 했는데... 댓글하단부에 빈 배열같은게 생긴거 같은데.. db내용은 들어가지가 않네요..!
글작성 에디터에서 하단부로 내려가는거는 지적해주셔서 수정하니까 정상작동 됩니다~!
-이미지 -
- TERMINAL 내용-
* Detected change in 'c:\\Users\\sosgx\\Desktop\\myweb\\main\\board.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 740-813-285
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://192.168.0.34:9000/ (Press CTRL+C to quit)
* Detected change in 'c:\\Users\\sosgx\\Desktop\\myweb\\main\\board.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 740-813-285
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://192.168.0.34:9000/ (Press CTRL+C to quit)
<pymongo.cursor.Cursor object at 0x000002A11A820358>
127.0.0.1 - - [25/Apr/2022 15:33:48] "GET /board/view/62663e6799e4b938f26b8aa0?page=1&search=-1&keyword= HTTP/1.1" 200 - * Detected change in 'c:\\Users\\sosgx\\Desktop\\myweb\\main\\board.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 740-813-285
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://192.168.0.34:9000/ (Press CTRL+C to quit)
{}
127.0.0.1 - - [25/Apr/2022 15:36:36] "GET /board/list HTTP/1.1" 200 -
127.0.0.1 - - [25/Apr/2022 15:36:37] "GET /board/write HTTP/1.1" 200 -
6266418622c4953c23dcdb14
127.0.0.1 - - [25/Apr/2022 15:36:54] "POST /board/write HTTP/1.1" 302 -
<pymongo.cursor.Cursor object at 0x0000021C73C67550>
127.0.0.1 - - [25/Apr/2022 15:36:54] "GET /board/view/6266418622c4953c23dcdb14 HTTP/1.1" 200 -
POST OK
{'root_idx': '6266418622c4953c23dcdb14', 'writer_id': '6265f517edcd20c927a2e8c1', 'name': '홍길동', 'comment': '정말 재
미있나요?', 'pubdate': 1650836227229}
127.0.0.1 - - [25/Apr/2022 15:37:07] "POST /board/comment_write HTTP/1.1" 302 -
<pymongo.cursor.Cursor object at 0x0000021C7455FA58>
127.0.0.1 - - [25/Apr/2022 15:37:07] "GET /board/view/6266418622c4953c23dcdb14 HTTP/1.1" 200 -
- 코드 -
-board.py-
남박사
지식공유자2022.04.25
지금 상황을 보면서 이해를 하실 필요가 있습니다.
위 이미지를 보면 플라스크 로그에는 일단 view 요청시 comments 가 print() 되는걸 확인할 수 있는데,
실제 HTML 쪽에서는 comments 의 갯수가 0으로 출력되는 상황입니다.
comment = mongo.db.comment
comments = comment.find({"root.idx": str(data.get("_id"))})
com_result = []
for c in comments:
com_result.append(c)
print(com_result)
return render_template("view.html", result=result, coms=com_result, page=page, search=search, keyword=keyword, title="글 상세보기")
일단 위의 코드처럼 comments 의 내용을 빈 리스트를 하나 만들어서 append 시키고 comments 변수명을 한번 변경해보는것도 좋을듯 합니다. 위 코드에서는 coms 라는 변수명으로 HTML에 데이터를 넘기게 수정되었습니다.
{{coms|length}}
{{coms}}
{% if coms|length > 0 %}
{% for c in coms%}
<div>
<table class="table">
<tr>
<td width="100"><h6>{{c.name}}h6>td>
<td>{{c_comment}}td>
<td class="text-right" width="200">{{c.pubdate | formatdatetime}}td>
tr>
table>
div>
{% endfor %}
{% endif %}
HTML 쪽에도 위의 코드처럼 변수명을 coms 로 변경해서 한번 처리해보시기 바랍니다. 그리고 지금 이 코드의 내용을 이해하셔야 합니다. 그래야 강좌의 진도를 맞춰가실 수 있습니다.
문신호
질문자2022.04.25
답변 감사합니다.
계속 물어봐서 죄송합니다..!
그래도 해결은 해야할꺼 같아서.. 계속 물어봅니다..ㅠ
아이디가 다를경우는 아까와 같은 값으로 나오는데요..!
-이미지1-
알려주신 방식으로 다시 수정을 해보았습니다.
아래 root.idx로 알려주셔서 해 보니까 값이 안떠서 root_idx로 변경하니까 값은 들어오는데.. 여러개가 들어오니까 알수가 없네요..!
-이미지2-
-TERMINAL-
127.0.0.1 - - [25/Apr/2022 17:07:40] "GET /board/list?page=1&search=-1&keyword= HTTP/1.1" 200 -
[{'_id': ObjectId('62662add897b71850a8b5877'), 'root_idx': '6262640e3ebba41f4b4f904b', 'writer_id': '6265f517edcd20c927a2e8c1', 'name': '홍길동', 'comment': '우왕!', 'pubdate': 1650830413380}, {'_id': ObjectId('62662ccb897b71850a8b587a'), 'root_idx': '6262640e3ebba41f4b4f904b', 'writer_id': '6265f517edcd20c927a2e8c1', 'name': '홍길동', 'comment': '남박사!',
'pubdate': 1650830907794}, {'_id': ObjectId('62662d28897b71850a8b587b'), 'root_idx': '6262640e3ebba41f4b4f904b', 'writer_id': '6265f517edcd20c927a2e8c1', 'name': '홍길동', 'comment': 'ㅁㄴㅇㅁㄴㅇ', 'pubdate': 1650831000605}, {'_id': ObjectId('62663e3b99e4b938f26b8a9f'), 'root_idx': '6262640e3ebba41f4b4f904b', 'writer_id': '6265f517edcd20c927a2e8c1', 'name': '홍길동', 'comment': '길동이!', 'pubdate': 1650835371866}]
- 코드 -
-board.py-
남박사
지식공유자2022.04.25
음.....
문제가 root_idx 를 root.idx 로 사용해서 생긴 문제였군요....
일단 질문을 자주 주시는건 아무 상관없습니다. 다만 현재 코드에 대한 이해를 조금 더 하셔야할 필요는 있어 보입니다. 물론 초보입장에서는 쉬운 문제가 아니지만 그래도 이해해보시는데 좀 더 시간을 투자해보시는것도 좋아보입니다.
comments = comment.find({"root_idx": str(data.get("_id"))})
지금까지의 문제는 위의 코드처럼 find 대상이 root_idx 인 요소의 이름이여야 하는데 root.idx 는 존재하지 않는 키 이름을 접근하는게 문제였던걸로 보입니다.
{{coms|length}}
{{coms}}
위코드는 오류를 찾기 위한 임시로 작성한 코드이므로 당연히 제거해야 하는 코드 입니다.
문신호
질문자2022.04.25
남박사님 답변 주셔서 감사합니다.
맨처음으로 돌아와서 다시 잘보고 수정하였는데요..!
남박사
지식공유자2022.04.25
네 해결하셨다니 다행입니다.
결과를 보는것도 중요하고 과정을 이해하는것도 중요하고.. 참 어렵습니다만.. 그래도 포기하지 않는게 가장 중요합니다. 궁금하신 사항은 뭐든 항상 질문주세요.
0
남박사
지식공유자2022.04.24
네 말씀하신대로 몽고디비 상위 버전에서는 더이상 커서에 count() 함수를 지원하지 않고 count_documents() 로 대체 되었습니다. 그런데 지금 코드에서는 갯수만 필요한게 아니라 커서를 갖고 데이터를 구하는 내용도 있기 때문에 단순히 count_documents() 함수로 처리하기가 좀 애매한 부분이 있습니다. 갯수와 데이터를 모두 사용해야 하기 때문에 일단,
{% if comments.count() > 0 %}
위 코드 부분을,
{% if len(list(cursor.clone())) > 0 %}
위와 같이 한번 수정해보시기 바랍니다.
문신호
질문자2022.04.24
안녕하세요 남박사님
말씀해주신대로 적용해보니까. cursor 찾을수 없다고 나오는데요.. cursor를 별도로 지정해주지 않아서 그런지 모르겠지만.. 오류가 다시 납니다..ㅠ
jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'cursor' is undefined
Traceback (most recent call last)
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 2091, in
__call__
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 2076, in
wsgi_app
response = self.handle_exception(e)
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 2073, in
wsgi_app
response = self.full_dispatch_request()
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 1518, in
full_dispatch_request
rv = self.handle_user_exception(e)
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 1516, in
full_dispatch_request
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 1502, in
dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
-
File "c:\Users\sosgx\Desktop\myweb\main\common.py", line 41, in
decorated_function
return f(*args, **kwargs)
-
File "c:\Users\sosgx\Desktop\myweb\main\board.py", line 150, in
board_view
return render_template("view.html", result=result, comments=comments, page=page, search=search, keyword=keyword, title="글 상세보기")
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\templating.py", line 150, in
render_template
ctx.app,
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\flask\templating.py", line 128, in
_render
rv = template.render(context)
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 1291, in
render
self.environment.handle_exception()
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 925, in
handle_exception
raise rewrite_traceback_stack(source=source)
-
File "c:\Users\sosgx\Desktop\myweb\main\templates\view.html", line 1, in
top-level template code
{% extends "main.html" %}
-
File "c:\Users\sosgx\Desktop\myweb\main\templates\main.html", line 48, in
top-level template code
{% block contents %}
-
File "c:\Users\sosgx\Desktop\myweb\main\templates\view.html", line 49, in
block 'contents'
{% if len(list(cursor.clone())) > 0 %}
-
File "C:\Users\sosgx\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 474, in
getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'cursor' is undefined
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump()
shows all variables in the framedump(obj)
dumps all that's known about the object
-----------------------------------------------------------------------
-코드-
-view.html-
남박사
지식공유자2022.04.24
{% if len(list(cursor.clone())) > 0 %}
# 커서객체를 이렇게 사용해야 한다는 의미였습니다.
{% if len(list(comments.clone())) > 0 %}
그대로 작성하시면 안되고.. 위 코드처럼 커서에 대한 변수명을 지정해서 해보시기 바랍니다.
문신호
질문자2022.04.25
남박사님 답변 주셔서 감사합니다..! 말씀해주신대로 해보니까 그래도 아직 오류가 나는 부분이 있어서 다시한번 질문 드립니다.
몽고DB버전을 바꿔야 하는건가요..ㅠ?
이번에는 list를 정의할수 없다고 하는데.. 어디 부분에서 만들어서 진행을 해야할까요..?
-오류-
jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'list' is undefined
Traceback (most recent call last)
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\app.py", line 2091, in
__call__
return self.wsgi_app(environ, start_response)
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\app.py", line 2076, in
wsgi_app
response = self.handle_exception(e)
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\app.py", line 2073, in
wsgi_app
response = self.full_dispatch_request()
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\app.py", line 1518, in
full_dispatch_request
rv = self.handle_user_exception(e)
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\app.py", line 1516, in
full_dispatch_request
rv = self.dispatch_request()
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\app.py", line 1502, in
dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
-
File "c:\Users\sosgx\Desktop\myweb\main\common.py", line 41, in
decorated_function
return f(*args, **kwargs)
-
File "c:\Users\sosgx\Desktop\myweb\main\board.py", line 150, in
board_view
return render_template("view.html", result=result, comments=comments, page=page, search=search, keyword=keyword, title="글 상세보기")
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\templating.py", line 150, in
render_template
ctx.app,
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\flask\templating.py", line 128, in
_render
rv = template.render(context)
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\jinja2\environment.py", line 1291, in
render
self.environment.handle_exception()
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\jinja2\environment.py", line 925, in
handle_exception
raise rewrite_traceback_stack(source=source)
-
File "c:\Users\sosgx\Desktop\myweb\main\templates\view.html", line 1, in
top-level template code
{% extends "main.html" %}
-
File "c:\Users\sosgx\Desktop\myweb\main\templates\main.html", line 48, in
top-level template code
{% block contents %}
-
File "c:\Users\sosgx\Desktop\myweb\main\templates\view.html", line 49, in
block 'contents'
{% if len(list(comments.clone())) > 0 %}
-
File "C:\Users\sosgx\Desktop\myweb\venv\lib\site-packages\jinja2\utils.py", line 84, in
from_obj
if hasattr(obj, "jinja_pass_arg"):
jinja2.exceptions.UndefinedError: 'list' is undefined
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump()
shows all variables in the framedump(obj)
dumps all that's known about the object
------------------------------------------------------------------------
-view.html-
답변 2