Как распечатать словарь в django
Я работаю над программой-переводчиком. Я хочу, чтобы при нажатии на кнопку 'submit' результат появлялся внизу исходной страницы.
Ошибки нет, но нет и изменений, когда я нажимаю кнопку 'submit'. Я хочу показать содержимое словаря с помощью таблицы.
Вот мои коды. 'trans_suc.html', 'views.py' и 'urls.py'. Пожалуйста, дайте мне знать, как их изменить.
trans_sub.html
{% extends 'dialect/base.html' %}
{% load static %}
<!-- template 확장하기 body interface -->
{% block content %}
<!-- Masthead-->
<div class = box>
<div class = "container">
<form action="/trans/" method ="post">
{% csrf_token %}
<input id = "trans" type ="text" name ="content" placeholder="0/500">
<input class = "btn" id="trans_btn" type ="submit" value ="번역">
</form>
<!--<div><p>{{ content }}</p>-->
{% for i in context %}
<div>
<table>
<tr>
<td>{{ i.siteName }}</td>
<td>{{ i.contents }}</td>
</tr>
</div>
{% endfor %}
</div>
</div>
{% endblock content %}
views.py
class Postview(View):
def get(self, request):
massage = "hello"
return render(request, 'dialect/main_page.html',
{'msg': massage}
)
@csrf_exempt
def success(request):
content = request.POST.get('content')
context = {
'content': content
}
dict = pd.read_csv("C:\\Users\\user\\jeju-dialect-translation\\jeju\\dialect\\dict2.csv", sep=",", encoding='cp949')
hannanum = Hannanum()
okt = Okt()
nouns = hannanum.nouns(content)
stem = okt.morphs(content, stem = True)
tempt=[]
for i in range(0, len(stem)):
if (len(stem[i]) == 1):
tempt.append(stem[i])
adjective = list(set(stem) - set(tempt))
results = pd.DataFrame(columns = {'siteName', 'contents'})
for i in nouns:
x = dict[dict['siteName'] == i]
x = x[['siteName', 'contents']]
results = pd.concat([results, x], axis = 0)
for i in adjective:
y = dict[dict['siteName'].str.match(i)]
results = pd.concat([results, y], axis = 0)
dropped_duplicates_result = results.drop_duplicates()
context = dropped_duplicates_result.to_dict()
return render(request, 'dialect/trans_suc.html',
context
)
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('dialect/', views.Postview.as_view(), name='main_page'),
path('trans/', views.Postview.success, name='trans_suc'),
path('dictionary/', views.Postview.dic, name='dictionary')
]
если это обычный словарь python, перебираем в цикле ключ и значение:
{% for key, value in context.items %}
{{ key }}{{ value }}
{% endfor %}
Думаю, вам стоит попробовать следующее:{% for key,value in context.items %}{{key}{value}}{% endfor %}
Вы должны попробовать следующее:{% for key, value in context.items %}{{key}}{{value}}{% end for %}