Reputation: 11
I am trying to develop a job portal where people can post jobs. I created a Listview and a DetailView for the application. The list view is working but the content isn't appearing on the details page.
views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Application_create
# Create your views here.
class AllApps(ListView):
model = Application_create
template_name = 'fundamentals/allapps.html'
ordering = ['-id']
class ApplicationInDetail(DetailView):
model = Application_create
template_name = 'fundamentals/application_detail.html'
urls.py:
from django.urls import path
from .views import AllApps, ApplicationInDetail
urlpatterns = [
path('allappslist', AllApps.as_view(), name='applist'),
path('app_in_detail/<int:pk>', ApplicationInDetail.as_view(), name='application_detail')
]
models.py:
from django.db import models
from django.urls import reverse_lazy
# Create your models here.
class Application_create(models.Model):
application_title = models.CharField(max_length=500, default='')
application_opening = models.DateTimeField(auto_now=True)
application_closing = models.DateField()
application_description = models.TextField(default='')
company_name = models.CharField(max_length=500, default='')
skills_req = models.CharField(max_length=2000, default='')
job_pay = models.IntegerField(default=10000)
freelancing_pay = models.IntegerField(default=10000)
job_type = models.CharField(max_length=500)
def __str__(self):
return self.application_title + self.company_name
def get_absolute_url(self):
return reverse('application_detail', args = [str(self.id)])
base.html:
<html>
<head>
{% load static %}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="{% static 'css/allappslist.css' %}">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light navbar_custom">
<a class="navbar-brand" href="#">
<img src="{% static 'images/expertoblogo.png' %}" width="40" height="40" alt="">
</a>
<a class="navbar-brand" href="#" >EXPERTOB</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" style="background-color: white;">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item" style="margin-right: 15px;">
<a class="nav-link" href="#" >Link</a>
</li>
<li class="nav-item" style="margin-right: 15px;">
<a class="nav-link" href="#" >Link</a>
</li>
<li class="nav-item" style="margin-right: 15px;">
<a class="nav-link" href="#" >Link</a>
</li>
</ul>
</nav>
{% block content %}
{% endblock %}
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
allapps.html:
{% extends 'fundamentals/base.html' %}
{% block content %}
{% for app in object_list %}
<ul>
<li class="list-group-item app_title" style="margin-left: 30px; margin-bottom: 0px; margin-right:30px; margin-top: 30px; background-color: #007FFF;">
<span style="color:white;"> {{app.application_title}} </span>
</li>
<li class="list-group-item list-group-item-light" style="margin-left: 30px; margin-bottom: 0px; margin-right:30px;">
<p>Company name: <span style="font-weight: bold;">{{app.company_name}} </span> </p>
</li>
<li class="list-group-item list-group-item-light" style="margin-left: 30px; margin-bottom: 0px; margin-right:30px;">
<p> {{app.application_description | slice:"0:200"}} ....... </p>
<p> <a href="{% url 'application_detail' app.pk %}"><button type="button" class="btn btn-primary">View Details</button> </a> </p>
</li>
</ul>
{% endfor %}
{% endblock %}
application_detail.html:
{% extends 'fundamentals/base.html' %}
{% block content %}
<h1> {{app.company_name}} </h1>
{% endblock %}
I've specified the model in views too but the content isn't appearing in the application_detail.html file. I'm using a mongo database which is working as the content is appearing in the allapps.html file. Please help me out
Upvotes: 1
Views: 706
Reputation: 331
In your list view you have {% for app in object_list %}
.
object_list
is Django's default naming of the list of objects in your view.
In your detail view you use app
, but this should be object
as that is Django's default naming of the object in the detail view.
The documentation is here https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-display/
Upvotes: 0
Reputation: 1211
I think you must add context_object_name
, change your code like below
class ApplicationInDetail(DetailView):
model = Application_create
template_name = 'fundamentals/application_detail.html'
context_object_name = 'app'
Upvotes: 1