I am new in Django and I am trying to do my project and I have an issue in that project. in my project, I have a view job button and whenever I click on any job or view job then it will redirect to another page and on that page, I want to show a full description of that job so please help me. thank you.
this is my views.py
def BrowseJob(request):
all_job = jobs.objects.all()
myFilter = jobsFilter(request.GET, queryset = all_job)
all_job = myFilter.qs
return render(request,'jobs.html',{'all_job':all_job,'myFilter':myFilter})
this is urls.py
from django.urls import path
from . import views
urlpatterns = [path('',views.index,name='index'),
path('Browse_Job',views.BrowseJob,name='Browse_Job'),
path('contact',views.contact,name='contact'),
path('job_details',views.jobdetails,name="job_details")]
this is my job.html
{% for job in all_job %}
<div class="job_lists">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="single_jobs white-bg d-flex justify-content-between">
<div class="jobs_left d-flex align-items-center">
<div class="thumb">
<img src="{{job.job_img.url}}" alt="">
</div>
<div class="jobs_conetent">
<h4>{{job.job_name}}</h4>
<div class="links_locat d-flex align-items-center">
<div class="location">
<p> <i></i>{{job.job_Category}}</p>
</div>
<div class="location">
<p> <i></i>{{job.job_Experience}}</p>
</div>
</div>
</div>
</div>
<div class="jobs_right">
<div class="apply_now">
View Job
</div>
<div class="date">
<p>Last Date for Apply: {{job.last_date}}</p>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
this is my jobdetails.html
{% for job in all_job %}
<img src="{{job.job_details.url}}" alt="">
{% endfor %}
please help me.
Actually to redirect in django using names in urls.py you need to write 'href' like this
<h4>{{job.job_name}}</h4>
Instead of
<h4>{{job.job_name}}</h4>
then it will work as you expected
refer this for more details :https://docs.djangoproject.com/en/3.1/ref/templates/builtins/
Related
This is a django related question:
I am doing an assignment where we are playing around making a search bar then afterwards letting each individual search result linked to its own page with more details. In this context we are doing a job search engine and I need assistance in when you click each jobs posting, it takes you to a separate page with more info about the job. We already made templates for all the pages. I understand that we have to make a request to the api again after doing it for the view function in the search bar and also use templating got fill up out the detailed search results.Im just not sure how would I apply these concepts to the html file that w ehave.
here's the code
View function code
import requests
from django.shortcuts import render
def home(request):
context = {
'example_context_variable': 'Change me.',
}
return render(request, 'pages/home.html', context)
def search_results(request):
search_query = request.GET['searchterm']
context = {
'result_count': 0,
'search_term': search_query,
}
context['results_count'] = 0
url = 'https://jobs.github.com/positions.json?location=bay+area&description='
url += search_query
response = requests.get(url)
results_data = response.json()
job_list =[]
for result in results_data:
job_list.append(result)
context['job_results'] = job_list
return render(request, 'pages/search_results.html', context)
Search Results Page
{% extends "base.html" %}
{% block title %}
Search Results
{% endblock title %}
{% block additional_styles %}
<style>
body {
background-color: white;
}
</style>
{% endblock %}
{% block content %}
<div id="home-content" class="container">
<div class="row">
<div class="col-lg-3"></div> <!-- Column for spacing -->
<div class="col-lg-6">
<div class="mt-5 mb-3 text-center">
<h1>Search Results</h1>
</div>
<form method="GET" action="/search-results/">
<div class="input-group mb-2">
<input name="searchterm" type="text" class="form-control form-control-lg" placeholder="Let's find a job..." />
<div class="input-group-append">
<button class="btn btn-primary btn-lg">Search</button></a>
</div>
</div>
</form>
<!-- 2start -->
<p>You Searched for: {{search_term}}</p>
{% for job in job_results %}
<div class="list-group">
<a href="/detailed-search-results/" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{job.title}}</h5>
</div>
<div>
<small class="text-muted">{{job.location}}</small>
</div>
</a>
</div>
<br>
{% endfor %}
<!-- End -->
{% endblock content %}
**detailed Search Results Html file **
So far when you click on a posting it takes you to the detailed search result page without anything on it.
That is because you have hyperlinked each job posting to /detailed-search-results/.
Looking at the API response, you need to change it to job.url
Replace your for loop with this:
{% for job in job_results %}
<div class="list-group">
<a href="{{ job.url }}" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{job.title}}</h5>
</div>
<div>
<small class="text-muted">{{job.location}}</small>
</div>
</a>
</div>
<br>
{% endfor %}
I'm attempting to use a for loop to iterate over the same data twice in the same Django template. However the second for loop only returns the first object in the database.
Basically I have an image of each job in the database on the homepage and when clicked on it should display a more detailed description of that job. All the jobs from the database display fine on the homepage but they all display the description of the first job when clicked on.
I think it's something to do with only being able to use an iterator once but I can't figure out what the solution is.
model.py
from django.db import models
class Job(models.Model):
title = models.CharField(max_length=50, default="Example Work")
image = models.ImageField(upload_to='images/')
summary = models.TextField()
views.py
from django.shortcuts import render
from .models import Job
def get_index(request):
jobs = Job.objects
return render(request, 'index.html', {'jobs': jobs})
index.html
{% for job in jobs.all %}
<div class="col-md-6 col-lg-4">
<a class="portfolio-item d-block mx-auto" href="#portfolio-modal">
<div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
<div class="portfolio-item-caption-content my-auto w-100 text-center text-white">
<i class="fas fa-search-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="{{ job.image.url }}" alt="">
</a>
<h3 class="text-center text-secondary">{{ job.title }}</h3>
</div>
{% endfor %}
<!-- Portfolio Modal -->
{% for job in jobs.all %}
<div class="portfolio-modal mfp-hide" id="portfolio-modal">
<div class="portfolio-modal-dialog bg-white">
<a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
<i class="fa fa-3x fa-times"></i>
</a>
<div class="container text-center">
<div class="row">
<div class="col-lg-8 mx-auto">
<h2 class="text-secondary text-uppercase mb-0">{{ job.title }}</h2>
<hr class="star-dark mb-5">
<img class="img-fluid mb-5" src="{{ job.image.url }}" alt="Image of website">
<p class="mb-5">{{ job.summary }}</p>
<a class="btn btn-primary btn-lg rounded-pill portfolio-modal-dismiss" href="#">
<i class="fa fa-close"></i>Close Project</a>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
Just for context, I've ripped out a lot of the html code from index.html because that all works fine and it probably isn't relevant. I've tried it in within one loop which doesn't work either. The description opens up within a new window on the same page. I've tried anything I can think of but not getting anywhere with it. Still getting to grips with Django and Python. Any help appreciated.
This isn't anything to do with Django outputting the same data for each iteration. If you check the generated HTML via View Source in your browser, you'll definitely see the correct data.
The problem is with your HTML itself. You cannot have multiple elements with the same HTML id attribute; but you are using id="portfolio-modal" for each one. When your JS tries to pop up the modal, it will only ever find the first instance of this ID.
You need to use a different ID in each iteration - perhaps by appending a unique element, eg the Job pk - and/or use a class to trigger your modal.
I'm trying to create a simple blog which shows various articles on the homepage. The index page as of now holds the title and sub-title of various articles. I want it to display the entire content of the article on click. This is the error that I'm running into on the homepage.
NoReverseMatch at /
Reverse for 'article' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<article_id>[0-9]+)$']
This is the content of urls.py in the pages app that I've created.
from django.urls import path
from . import views
urlpatterns=[
path('',views.index,name='index'),
path('<int:article_id>',views.article,name='article'),
path('about',views.about,name='about'),
]
This is my views.py
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from . models import Post
# Create your views here.
def index(request):
post=Post.objects.all()
context = {
'post' : post
}
return render(request,'pages/index.html',context)
def article(request,article_id):
article=get_object_or_404(Post,pk=article_id)
context = {
'article' : article
}
return render(request,'pages/article.html',context)
def about(request):
return render(request,'pages/about.html')
As you can probably see, I'm referring to the content of the articles through an article_id and the data regarding the particular post is fetched from the database.
This is my index.html, which should redirect to the content of the specific post on click.
{%extends 'base.html'%}
{%load static%}
{%block content%}
<!-- Page Header -->
<header class="masthead" style="background-image: url({% static 'img/home-bg.jpg' %})">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Clean Blog</h1>
<span class="subheading">A Blog Theme by Start Bootstrap</span>
</div>
</div>
</div>
</div>
</header>
<!-- Posts -->
{% if post %}
{% for posts in post %}
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-preview">
<a href="{% url 'article' article.id%}">
<h2 class="post-title">
{{posts.title}}
</h2>
{%if posts.subtitle%}
<h3 class="post-subtitle">
{{posts.subtitle}}
</h3>
{%endif%}
</a>
<p class="post-meta">Posted by
{{posts.postby}}
on {{posts.date}}</p>
</div>
<hr>
</div>
</div>
</div>
{% endfor %}
{%endif%}
<!-- Pager -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="clearfix">
<a class="btn btn-primary float-right" href="#">Older Posts →</a>
</div>
</div>
</div>
</div>
<hr>
{%endblock%}
However though, I'm getting the required page when I type in localhost:8000/1 or localhost:8000/2 manually just like how I want it. But the problem is that, it does not redirect on click. My best guess is that this
<a href="{% url 'article' article.id%}">
is creating the problem.
All suggestions are welcome!.
Thank you.
You haven't got anything called "article" in that template. Your object is called "posts". So:
<a href="{% url 'article' posts.id %}">
(We'll leave aside the question of why you're calling your collection of posts "post" and each individual post "posts"...)
I'm currently building a blog+portfolio with Django and Bootstrap 4.
The problem is when I try to show two columns, Articles next to my Profile. Both columns are in an HTML table, but for some reason, they do not show correctly.
For example:
How it looks like using HTML,CSS, and Bootstrap
Here is how it looks when I insert everything on my Base_layout.html
How it looks like when it is inserted in my django Base_layout.html
Here is my Base_layout, which has the code inside the table to show the articles of the blog ({% load bootstrap4 %} {% load static from staticfiles %} are included in the top of the document):
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8">
<div class="card mb-4" class="article-detail">
{% block content %}
{% endblock %}
</div>
</div>
<!-- Pagination -->
<ul class="pagination justify-content-center mb-4">
<li class="page-item">
<a class="page-link" href="#">← Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#">Newer →</a>
</li>
</ul>
</div>
<!-- Sidebar Widgets Column -->
<div class="col-md-4">
<div class="card" style="width: 18rem;">
<img src="{% static 'profile.jpg' %}" class="rounded-circle card-img-top" alt="">
<div class="card-body">
<h5 class="card-title">Leandro Fraisinet</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
More
</div>
</div>
</div>
</div>
</div>
Here is the code that is to be shown by Blog Entries Column
Article_list html:
{% extends 'base_layout.html' %}
{% load bootstrap4 %}
{% block content %}
<h1> </h1>
{% for article in articles %}
<div class="card-body" class="article">
<img class="card-img-top" src="{{ article.thumb.url}}" alt="Main post image">
<h2 class="card-title">{{article.title}}</h2>
<p class="card-text">{{article.snippet}}</p>
</div>
<div class="card-footer text-muted">
<p>{{article.date}}</p>
</div>
<h1> </h1>
{% endfor%}
{% endblock %}
I think that my main conflict is in Blog Entries Column but I do not know how to solve this.
Please any extra code that could be useful to solve this let me know.
Django version, 1.11 Phyton version 2.7, Bootstrap version 4.
You sidebar column is outside the row
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8">
</div>
<!-- Pagination -->
<ul class="pagination justify-content-center mb-4">
</ul>
</div>
<!-- Sidebar Widgets Column -->
<div class="col-md-4">
</div>
try to put it inside:
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8"></div>
<!-- Sidebar Widgets Column -->
<div class="col-md-4"></div>
</div>
<!-- Pagination -->
<ul class="pagination justify-content-center mb-4"></ul>
</div>
I'm trying to apply a parallax template to my django blog but i seem to be running into a snag
class BlogIndex(generic.ListView):
queryset = models.BlogPost.objects.published()
template_name = "blog/blogindex.html"
paginate_by = 5
I have this view in my views for my blog app.
Now the problem is I four divs I want to split this view into and render into the template.
<section class="module parallax parallax-1">
<div class="container">
</div>
</section>
<section class="module content">
<div class="container">
{{ view goes here }}
</section>
<section class="module parallax parallax-2">
<div class="container">
</div>
</section>
<section class="module content">
<div class="container">
{{ view goes here }}
</div>
</section>
<section class="module parallax parallax-3">
<div class="container">
</div>
</section>
<section class="module content">
<div class="container">
{{ view goes here }}
</div>
</section>
<section class="module parallax parallax-4">
<div class="container">
</div>
what would be the best way to render my blog posts in each div?
You may be able to get the desired effect by taking advantage of the for loop counters.
Django's {% for %} template generates custom variables within the loop. These include:
forloop.counter: The current iteration of the loop (1-indexed)
forloop.counter0: The current iteration of the loop (0-indexed)
forloop.revcounter: The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0: The number of iterations from the end of the loop (0-indexed)
forloop.first: True if this is the first time through the loop
forloop.last: True if this is the last time through the loop
forloop.parentloop For nested loops, this is the loop surrounding the current one
(via https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for)
Using your example, you may be able to do something like this
{% for object in object_list %}
<section class="module parallax parallax-{{ forloop.counter }}">
<div class="container">
</div>
</section>
<section class="module content">
<div class="container">
{{ object }}
</div>
</section>
{% endfor %}