简体   繁体   English

来自数据库的数据未加载到 HTML 表 Django

[英]Data From Database Not Loading into HTML table Django

I would like to create a table that lists the rows from a small database (7 rows including header, 3 columns) to an HTML table.我想创建一个表格,其中列出了从小型数据库(包括标题、3 列在内的 7 行)到 HTML 表格的行。 I am working in Django and have checked "myapp"views.py, "myapp".urls, and the base.html.我在 Django 工作并检查了“myapp”views.py、“myapp”.urls 和 base.html。 I also checked myprojects url settings and did not see anything.我还检查了 myprojects url 设置,但没有看到任何内容。

Any help would be great.任何帮助都会很棒。

Windows 10 Django 2.2.7 Python 3.8 Windows 10 Django 2.2.7 Python 3.8

My code is below:我的代码如下:

my\\app view.py我的\\应用程序视图.py

def repList(request):
    all_objects_Rep=Rep.objects.all()
    context={'all_objects_Rep': all_objects}
    return render (request, 'base.html', context)

myproject\urls.py

urlpatterns = [

        path('', include('page.urls')),
        path('rep/', include('page.urls', namespace='reps')),
        path('home/', include('page.urls', namespace='home')),
        path('results/', include('page.urls', namespace='results')),
        path('admin/', admin.site.urls),

myapp\\urls.py myapp\\urls.py

app_name = 'page'
urlpatterns = [
    path('', views.homepage_view, name='homepage_view'),
    path('Rep/', views.repList, name='repList'),
    path('userInput/', views.userInput, name ='User Input'),
    path('results/', views.results_view, name='results_view'),



myapp\base.html

    <div class ="RepList">
                {% block repList %}
            <table>
                <tr>
                    <th>District</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>

                {% for x in all_objects_Rep %}
                <tr>
                    <td>{{ x.District }}</td>
                    <td>{{ x.f_name }}</td>
                    <td>{{ x.l_name }}</td>
                </tr>
                {% endfor %}
                {% endblock %}
            </table>
                </div>```


You URL configuration is not quite right - you should only include page.urls once, not multiple times.您的 URL 配置不太正确 - 您应该只包含page.urls一次,而不是多次。 Change your project URL configuration to:将您的项目 URL 配置更改为:

urlpatterns = [
    path('', include('page.urls')),
    path('admin/', admin.site.urls),
]

Then if you visit /Rep , which is the URL you have defined for your repList view, you should see the correct contents being rendered.然后,如果您访问/Rep ,这是您为repList视图定义的 URL,您应该会看到正在呈现的正确内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM