Federico De Marco
Federico De Marco

Reputation: 341

Django: How can display in the same url form and table?

I'm learning to programming in django. For the moment I'm building a simple app that utilizing a form update the referenced table.

All works untill I assign two different urls for table and forms, but If I try to merge them togheter in the same url (utilizing so the same html file) django give me the following error:

ValueError at /
Expected table or queryset, not str
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.1.5
Exception Type: ValueError
Exception Value:    
Expected table or queryset, not str

Above my code in each file of my project:

urls



from django.urls import path
from app import views


urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('', views.PersonListView.as_view(), name='algo'),

]

forms

from django import forms
from .models import Income

class IncomeModelForm(forms.ModelForm):

    class Meta:
        model = Income
        fields = "__all__"

tables

import django_tables2 as tables
from .models import Income

class PersonTable(tables.Table):
    class Meta:
        model = Income
        template_name = "django_tables2/bootstrap.html"

views

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView
from .models import Income
from .tables import PersonTable
from django_tables2 import SingleTableView
from .forms import IncomeModelForm



def homepage(request):
    if request.method == 'POST':
         form = IncomeModelForm(request.POST)
         if form.is_valid():
             print("Il form è valido")
             new_post = form.save()
    else :
        form = IncomeModelForm()

    context= {"form": form }
    return render(request, "app/base.html", context)


class PersonListView(SingleTableView):
    model = Income
    table_class = PersonTable
    template_name = 'app/base.html'

html

{% load static %}
{% load render_table from django_tables2 %}
<!doctype html>
<html lang="it">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>

    <br>
    <br>
<div class="container">
        <form class="" action="" method="post">
            {% csrf_token %}
            {{form.as_p}}
            <input type="submit" class="btn btn-danger" value="INVIA">
        </form>
</div>

<div class="container">
    {% render_table table %}
</div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

  </body>
</html>

Upvotes: 0

Views: 274

Answers (1)

Thibault J
Thibault J

Reputation: 4446

You can't have the same url associated to different views. Look at it from the Django's point of view: when the user requests the '/' url, should it route to the 'homepage' or 'algo' view?

You want something like this:

urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('persons/', views.PersonListView.as_view(), name='person_list'),
]

If you want a single url the display both the form and the table, then you need a single view that both handles the form and the table.

Upvotes: 1

Related Questions