Reputation: 1
I am new to Django. I have to create multiple views and create buttons for them in the main page. I have created different view for different filters of the table. I now have to create buttons in main page to redirect to different views . Can't figure the changes in urls.py and templates.
here is my code :
views.py
from django_tables2 import SingleTableView
from django.db.models import Max
from django.db.models import Min
from .models import airdata
from .tables import AirdataTable
class AirdataListView(SingleTableView):
model = airdata
table_class = AirdataTable
template_name = 'dashboard/data.html'
q = airdata.objects.aggregate(alpha=Min('pol_max'))['alpha']
queryset = airdata.objects.filter(pol_min= q).order_by('-state')
class AirdataListView2(SingleTableView):
model = airdata
table_class = AirdataTable
template_name = 'dashboard/data2.html'
q = airdata.objects.aggregate(alpha=Max('pol_max'))['alpha']
queryset = airdata.objects.filter(pol_min= q).order_by('-state')
app/urls.py
from django.urls import path
from . import views
from .views import AirdataListView,AirdataListView2
urlpatterns = [
path('page1/', AirdataListView2.as_view()),
path('page2/', AirdataListView.as_view()),
]
project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('dashboard/', include('dashboard.urls')),
path('admin/', admin.site.urls),
]
template
{% load render_table from django_tables2 %}
<!doctype html>
<html>
<head>
<title>Air Data</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
{% render_table table %}
</body>
</html>
Upvotes: 0
Views: 674
Reputation: 126
Django will try to match the given url with one of the paths defined in urls.py
.
In your case, you have two views that match the empty pattern, so Django is returning the first match and you wont be able to access your second pattern.
Try this on your urls.py
:
urlpatterns = [
path('page1/', AirdataListView2.as_view()),
path('page2/', AirdataListView.as_view()),
]
Chances are you will eventually want to do more complex stuff and pass parameters into your urls so perhaps its a good idea to check the docs for more info about them.
Upvotes: 2
Reputation: 2334
Sure, as Django will run the first view match the url pattern which is the first one in your case. The problem you are handling can be solved by GET parameters. So read about it.
Upvotes: 0