I am unable to display values from DB on home page. I can't figure out what I've done wrong. Following is my html
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" async></script>
</head>
<div class="container">
{% for manhour in manhours %}
<div class="article-metadata">
<a class="mr-2" href="#">{{ manhour.station }}</a>
<small class="text-muted">{{ manhour.date|date:"F d, Y" }}</small>
</div>
<h2><a class="article-title" href="{% url 'manhour-detail' manhour.id %}">{{ manhour.station }}</a></h2>
{% endfor %}
</div>
<div class="user_panel">
<a href="/logout">logout</a>
</div>
<div class="user_panel">
<a href="/manhour/new">Upload</a>
</div>
</html>
view.py
# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import (
ListView,
DetailView,
CreateView,
UpdateView,
DeleteView
)
from .form import InputForm
from manhour.models import ManHour
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib import messages
from django.contrib.auth import login, logout, authenticate
from django.urls import reverse
def login_request(request):
if request.method == 'POST':
form = AuthenticationForm(request=request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
messages.info(request, f"You are now logged in as {username}")
return redirect('manhour-home')
else:
messages.error(request, "Invalid username or password.")
else:
messages.error(request, "Invalid username or password.")
else:
form = AuthenticationForm()
return render(request = request,
template_name = "manhour/login.html",
context={"form":form})
class PostListView(LoginRequiredMixin, ListView):
model = ManHour
template_name = 'manhour/home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'manhour'
# ordering = ['-date']
class PostDetailView(LoginRequiredMixin, DetailView):
model = ManHour
urls.py
from django.contrib import admin
from django.urls import path
# from manhour.views import login_request
from manhour.views import (
PostListView,
PostDetailView,
PostCreateView,
PostUpdateView,
login_request
)
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', login_request, name='login'),
path('', login_request, name='login'),
path('manhour/', PostListView.as_view(), name='manhour-home'),
path("logout/", LogoutView.as_view(), name="logout"),
path('manhour/new/', PostCreateView.as_view(), name='manhour-create'),
path('manhour/<int:pk>/', PostDetailView.as_view(), name='manhour-detail'),
path('manhour/<int:pk>/update/', PostUpdateView.as_view(), name='manhour-update'),
]
Can anyone identify why I can't list all values on my manhour-home
page? On home page I can see logout
and upload
button which is working fine. But I am unable to view all the objects from database on home page.
Update I've added following into views.py
def home(request):
context = {
'manhour': ManHour.objects.all()
}
return render(request, 'manhour/home.html', context)
You haven't defined what needs to be displayed in the view. You can either define a queryset attribute for the view, or define get_queryset() method.
Your context object name is wrong, in views you are using "manhour" and in your home template you are using "manhours".
So, I think this should fix it.
def home(request):
context = {
'manhours': ManHour.objects.all()
}
return render(request, 'manhour/home.html', context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.