0

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. I am working in Django and have checked "myapp"views.py, "myapp".urls, and the base.html. I also checked myprojects url settings and did not see anything.

Any help would be great.

Windows 10 Django 2.2.7 Python 3.8

My code is below:

my\app view.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

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>```


1 Answer 1

1

You URL configuration is not quite right - you should only include page.urls once, not multiple times. Change your project URL configuration to:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When I changed the suggested change from myproject\urls.py I received the following error: ``Using the URLconf defined in kentucky.urls, Django tried these URL patterns, in this order: [name='homepage_view'] Rep/ [name='repList'] userInput/ [name='User Input'] results/ [name='results_view'] admin/ ^static/(?P<path>.*)$ The current path, home/, didn't match any of these.
That'e because you're trying to visit home/. As I noted in my answer, the URL you have defined for this view is /Rep - so you need to visit that URL to see the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.