Python Django未在模板中提供HTML表(word计数器web应用程序)
摘要: Django没有呈现一个基本的2列11行HTML表格,该表格包含大型文本文件中最常用的10个单词(Alice and Wonderland,这是一个公共领域的作品)。但是现在桌子是空的 详细信息: 当前在网页上呈现的表如下所示。在那张图中,注意右下角,桌子是空的 当web用户导航到Python Django未在模板中提供HTML表(word计数器web应用程序),python,django,Python,Django,摘要: Django没有呈现一个基本的2列11行HTML表格,该表格包含大型文本文件中最常用的10个单词(Alice and Wonderland,这是一个公共领域的作品)。但是现在桌子是空的 详细信息: 当前在网页上呈现的表如下所示。在那张图中,注意右下角,桌子是空的 当web用户导航到http://127.0.0.1:8000/counters 然而,第二张图片中的问题是博客文章“lorem ipsum”的内容是空的 这是我的父母的最高级别: from django.contrib impo
http://127.0.0.1:8000/counters
然而,第二张图片中的问题是博客文章“lorem ipsum”的内容是空的
这是我的父母的最高级别:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# path('', include('mortems.urls')),
path('', include('redactors.urls')),
path('', include('posts.urls')),
path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.shortcuts import render
from collections import Counter
import re
from nltk.corpus import stopwords
def counters(request, text=""):
"""
This function processes the top 10 most common words in a text file and then renders them.
"""
text = open("counters/Alice.txt", "r").read().lower()
stoplist = stopwords.words('english')
stoplist.extend(["said","gutenberg", "could", "would",])
clean = []
for word in re.split(r"\W+",text):
if word not in stoplist:
clean.append(word)
top_10 = Counter(clean).most_common(10)
return render(request, 'alls/landings.html', {'top_10': top_10})
from django.urls import path, include
from . import views
urlpatterns = [
path('counters', views.counters, name='counters'),
]
<h1>Counting the words!</h1>
This should only show inside landing.html
<div class="">
<table>
<tr><th>Word</th> <th>Quantity</th></tr>
{% for word, count in top_10 %}
<tr> <td>{{word}} </td><td>{{count}} </td></tr>
{% endfor %}
</table>
</div>
这是我的计数器
应用程序的:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# path('', include('mortems.urls')),
path('', include('redactors.urls')),
path('', include('posts.urls')),
path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.shortcuts import render
from collections import Counter
import re
from nltk.corpus import stopwords
def counters(request, text=""):
"""
This function processes the top 10 most common words in a text file and then renders them.
"""
text = open("counters/Alice.txt", "r").read().lower()
stoplist = stopwords.words('english')
stoplist.extend(["said","gutenberg", "could", "would",])
clean = []
for word in re.split(r"\W+",text):
if word not in stoplist:
clean.append(word)
top_10 = Counter(clean).most_common(10)
return render(request, 'alls/landings.html', {'top_10': top_10})
from django.urls import path, include
from . import views
urlpatterns = [
path('counters', views.counters, name='counters'),
]
<h1>Counting the words!</h1>
This should only show inside landing.html
<div class="">
<table>
<tr><th>Word</th> <th>Quantity</th></tr>
{% for word, count in top_10 %}
<tr> <td>{{word}} </td><td>{{count}} </td></tr>
{% endfor %}
</table>
</div>
这是我的计数器
应用程序的:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# path('', include('mortems.urls')),
path('', include('redactors.urls')),
path('', include('posts.urls')),
path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.shortcuts import render
from collections import Counter
import re
from nltk.corpus import stopwords
def counters(request, text=""):
"""
This function processes the top 10 most common words in a text file and then renders them.
"""
text = open("counters/Alice.txt", "r").read().lower()
stoplist = stopwords.words('english')
stoplist.extend(["said","gutenberg", "could", "would",])
clean = []
for word in re.split(r"\W+",text):
if word not in stoplist:
clean.append(word)
top_10 = Counter(clean).most_common(10)
return render(request, 'alls/landings.html', {'top_10': top_10})
from django.urls import path, include
from . import views
urlpatterns = [
path('counters', views.counters, name='counters'),
]
<h1>Counting the words!</h1>
This should only show inside landing.html
<div class="">
<table>
<tr><th>Word</th> <th>Quantity</th></tr>
{% for word, count in top_10 %}
<tr> <td>{{word}} </td><td>{{count}} </td></tr>
{% endfor %}
</table>
</div>
注意path函数中的第一个参数。这就是问题所在,因为现在web访问者在导航到127.0.0.1:8000/计数器时会看到该表,但我试图让Django在用户导航到127.0.0.1:8000/
或127.0.0.1:8000/主页时提供该表。因此,当我将第一个'counters'
参数更改为'home'
或'
(保留为空)时,表根本不会呈现
最后,我要做的是让Django提供计数器
功能,并在网站访问者导航到http://127.0.0.1:8000/home
或http://127.0.0.1:8000/
。这就是我正在努力实现的目标。但是现在要查看该表,用户需要访问http://127.0.0.1:8000/counters
没有博客文章内容的地方
在我的Django服务器日志中没有回溯,所以我没有太多的Google搜索词可供使用
顺便说一句,这是我的缩略(简化)HTML表,它显示在我的中:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# path('', include('mortems.urls')),
path('', include('redactors.urls')),
path('', include('posts.urls')),
path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.shortcuts import render
from collections import Counter
import re
from nltk.corpus import stopwords
def counters(request, text=""):
"""
This function processes the top 10 most common words in a text file and then renders them.
"""
text = open("counters/Alice.txt", "r").read().lower()
stoplist = stopwords.words('english')
stoplist.extend(["said","gutenberg", "could", "would",])
clean = []
for word in re.split(r"\W+",text):
if word not in stoplist:
clean.append(word)
top_10 = Counter(clean).most_common(10)
return render(request, 'alls/landings.html', {'top_10': top_10})
from django.urls import path, include
from . import views
urlpatterns = [
path('counters', views.counters, name='counters'),
]
<h1>Counting the words!</h1>
This should only show inside landing.html
<div class="">
<table>
<tr><th>Word</th> <th>Quantity</th></tr>
{% for word, count in top_10 %}
<tr> <td>{{word}} </td><td>{{count}} </td></tr>
{% endfor %}
</table>
</div>
数单词!
这应该只显示在landing.html内部
字数
{单词的百分比,计入前10%}
{{word}}{{count}
{%endfor%}
以下是我在GitHub上的完整源代码。我认为问题在于你设计顶级URL的方式。如果希望用户在访问主页后立即重定向到计数器视图,则需要将父URL.py更改为以下内容:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# path('', include('mortems.urls')),
path('redactors', include('redactors.urls')),
path('posts', include('posts.urls')),
path('', include('counters.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
您的版本中存在的问题是,一旦用户访问主页,他就会被定向到redactors.url,因为这是主页url的第一个匹配项
接下来,您需要将counters.url更改为:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.counters, name='counters'),
]
这将在用户访问您的网页时直接将其重定向到计数器视图
或者,如果您希望在http://127.0.0.1:8000/home
,然后您可以将父URL.py更改为:
path('home/', include('counters.urls')),
尤里卡!它起作用了
自上次以来,counters/views.py
已重新排列。该视图中包含的函数已移动到posts/views.py
中。即使是top\u word\u counts
函数也已被移动到位于posts/utils.py
的新文件中。以下是imgur上的屏幕截图,其中显示了Alice.txt(左下角)中最常出现的单词:
您可能会注意到新功能的痕迹。例如,我的应用程序不仅计算Alice.txt的内容,现在还计算博客帖子中最常见的单词(现在是lorem ipsum占位符内容)
考虑到所有这些变化,下面是我的工作posts/views.py
:
import operator
from django.shortcuts import redirect, render, get_object_or_404
from posts.models import Posts
from .utils import top_word_counts
def posts(request):
posts = Posts.objects.all().order_by('-pub_date')
context = {'posts':posts}
post_string = ''
for post in posts:
post_string += post.body
post_words = top_word_counts(post_string.lower())
alice_words = top_word_counts(
open("counters/Alice.txt", "r").read().lower())
context.update({
'post_words': post_words,
'alice_words': alice_words
})
return render(request, 'alls/landings.html', context)
import re
from collections import Counter
from nltk.corpus import stopwords
def top_word_counts(text):
# text = open("counters/Alice.txt", "r").read().lower()
stoplist = stopwords.words('english')
stoplist.extend(["said", "gutenberg", "could", "would", ])
clean = []
for word in re.split(r"\W+", text):
if word not in stoplist:
clean.append(word)
top_10 = Counter(clean).most_common(10)
return top_10
这是我新的帖子/utils.py
:
import operator
from django.shortcuts import redirect, render, get_object_or_404
from posts.models import Posts
from .utils import top_word_counts
def posts(request):
posts = Posts.objects.all().order_by('-pub_date')
context = {'posts':posts}
post_string = ''
for post in posts:
post_string += post.body
post_words = top_word_counts(post_string.lower())
alice_words = top_word_counts(
open("counters/Alice.txt", "r").read().lower())
context.update({
'post_words': post_words,
'alice_words': alice_words
})
return render(request, 'alls/landings.html', context)
import re
from collections import Counter
from nltk.corpus import stopwords
def top_word_counts(text):
# text = open("counters/Alice.txt", "r").read().lower()
stoplist = stopwords.words('english')
stoplist.extend(["said", "gutenberg", "could", "would", ])
clean = []
for word in re.split(r"\W+", text):
if word not in stoplist:
clean.append(word)
top_10 = Counter(clean).most_common(10)
return top_10
谢谢@Hatim,但我正试图在Django提供landings.HTML
模板时显示计数器
HTML表,而不是单独在一个模板中提供。这三个应用程序(redactors
,posts
,counters
)都应该显示在网站的登录页http://127.0.0.1:8000/
而不是在单独的页面上,例如http://127.0.0.1:8000/counters
或http://127.0.0.1:8000/redactors
这就是路径参数的作用。接下来我将更新我的问题以进一步澄清这一点。好的,我从您的评论中了解到,计数器表是登录页的一部分,而不是整个登录页,对吗?加载页面后,可以使用AJAX调用呈现计数器表。如果我正确理解了你的问题,请告诉我。