created a simple search in the Django blog but it's not working. why search bar is not working in a blog web app ? its nothing to search in the search bar when searching something in the search box.
- urls.py
urlpatterns = [
path('', views.home, name='home'),
path('about', views.about, name='about'),
path('contact', views.contact, name='contact'),
path('search', views.search, name='search'),
]
- views.py
def search(request):
query = request.GET['query']
allPosts = Post.objects.filter(title__icontains=query)
params = {'allPosts': allPosts}
return render(request,'home/search.html', params)
- search.html
{% extends 'base.html' %}
{% block title %} Search Results {% endblock title %}
{% block blogactive %}active{% endblock blogactive %}
{% block body %}
<div class="container my-3">
<h2>Search Results</h2>
{% for post in allposts %}
<div class="row no-gutters border rounded overflow-hidden flex-md-row my-4 shadow-sm h-md-250 position-relative">
<div class="col p-4 d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-primary">Article by {{post.author}}</strong>
<h3 class="mb-0">{{post.title}}</h3>
<div class="mb-1 text-muted">{{post.datetime}}</div>
<p class="card-text mb-auto">{{post.content | truncatechars:500}}</p>
<div class='my-2'>
Continue reading
</div>
<div class="col-auto d-none d-lg-block">
</div>
</div>
</div>
{% endfor %}
{% endblock body %}
There is a typo in your code. In template you are using allposts(all lower case) where you are passing allPosts from context, ie: params = {'allPosts': allPosts}. So you need to change either one of them, like change in context:
params = {'allposts': allPosts}
And one improvement suggestion, replace href="/blog/{{post.slug}}" in the template with href="{% url 'url_name' %}". More information can be found in url tag documentation.
Related
I am getting a typeerror my_view() got an unexpected keyword argument 'pk'
views.py:
def my_view(request):
prod = get_object_or_404(Products, pk=1)
context = {
'prod': prod,
}
return render(request, 'title.html', context)
urls.py:
urlpatterns = [
path('search/', views.search, name='search'),
path('<int:pk>', views.my_view, name='my_view'),
path('', views.index),
]
My template when I make a search:
{% if results.products %}
{% for product in results.products %}
<div class="col-sm-{% column_width results.products %} pb-5">
<div class="card">
<div class="card-header bg-default" align="center">
<b>{{product.title}}</b>
</div>
<div class="card-body">
<a href="{% url 'my_view' product.id %}">{{
product.Author }}</a>
<p>{{ product.description|striptags }}</p>
<h6 class="btn btn-primary btn-large">
{{product.price}}</h6>
</div>
</div>
</div>
{% endfor %}
{% endif %}
My template to render when making a click on one of the search:
<div class="row">
<div class="col-md-12">
<div class="jumbotron">
<h2>{{ prod.title }}</h2>
<p>{{ prod.description }}</p>
<h6 class="btn btn-primary btn-large">{{judi.price}}</h6>
<h6 style="float: right;">{{judi.modified_date}}</h6>
</div>
</div>
</div>
You need to tell your
view that, a parameter named pk will come alongside the request.
You need to change:
def my_view(request):
with
def my_view(request, pk)
Furthermore, in your urls.py you need to give a path to your view, your current path lacks a path string. You need to replace:
path('<int:pk>', views.my_view, name='my_view') with:
path('my_view/<int:pk>', views.my_view, name='my_view')
Also in your template replace:
<a href="{% url 'my_view' product.id %}"> with
<a href="{% url 'my_view' pk=product.id %}">
I am trying to create the URL in order to access the EDIT and DELETE views directly from the post detail instead of typing it in the browser.
I am having trouble finding the right url pattern and template {% url %} code since there is a slug.
posts.urls
urlpatterns = [
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create),
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<slug>[\w-]+)/delete/$', post_delete, name='delete'),
post_detail.html
{% block content %}
<div class='col-sm-6 col-sm-offset-3'>
{% if instance.image %}
<img src='{{ instance.image.url }}' class='img-responsive' />
{% endif %}
<h1>
{{ title }}
<small>
{% if instance.draft %}
<span style='color:red;'>Draft</span>
{% endif %}{{ instance.publish }}
<div class=''>
Edit |
Delete
</div>
</small>
</h1>
You need to pass the slug into the url tag in the html.
Try something like this,
Edit
Delete
My use case or what I want to do is:
Users click a Upload button,
it authenticates user,
its shows Instagram images,
users select, and
user click upload button to upload images or videos (media) from
Instagram directly into my django app.
What I have done is:
Instagram integration:
pip install python-instagram
views.py:
#login_required()
def instagram_pictures(request, template='dashboard/add-instagram-pictures.html'):
user = request.user
gallery = []
gallery = Gallery.objects.filter(service_provider=user.service_provider_profile)
context = {'gallery': gallery}
return render(request, template, context)
#login_required()
def add_instagram_pictures(request, template='dashboard/add-instagram-pictures.html'):
access_token = "YOUR_ACCESS_TOKEN" # Dont know how this gets factored in for any user
client_secret = settings.SECRET_ID
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
for media in recent_media:
print(media.caption.text)
template/my-gallery.html:
{% extends 'base.html' %}
{% load static %}
{% block title %}My Dashboard{% endblock %}
{% block content %}
<div class="page-header" style="background: url({% static 'img/banner1.jpg' %});">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="page-title">My Gallery</h1>
</div>
</div>
</div>
</div>
<div id="content">
<div class="container">
<div class="row">
{% include '_side_menu.html' %}
<div class="col-md-8 page-content">
<div class="inner-box text-center">
<a href="{% url 'dashboard:add-instagram-pictures' %}" class="btn btn-md btn-success">
<span class="fa fa-plus-circle"> Upload from Instagram</span>
</a>
<ul>
<!-- I will re-write this loop to redefine how images are rendered for the project. Maybe in Carousels-->
{% for p in photos %}
<li>{{ p.name }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
template/add-instagram-pictures.html:
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Gallery{% endblock %}
{% block content %}
<div class="page-header" style="background: url({% static 'img/banner1.jpg' %});">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="page-title">Add Instagram Pictures</h1>
</div>
</div>
</div>
</div>
<div id="content">
<div class="container">
<div class="row">
{% include '_side_menu.html' %}
<div class="col-md-8 page-content">
<div class="inner-box text-center">
<a href="{% url 'dashboard:my-gallery' %}" class="btn btn-md btn-success">
<span class="fa fa-chevron-circle-left"> Back to Album List</span>
</a>
</div>
</div>
</div>
</div>
</div> {% endblock %}Add
url.py:
from django.conf.urls import url, include
from dashboard.views import dashboard, my_services, add_service, my_gallery, add_gallery, bank_profile, add_bank_profile, add_instagram_pictures
def ajax_photo_upload_view(args):
pass
urlpatterns = [
url(r'^$', dashboard, {}, name='home'),
url(r'^bank-profile/$', bank_profile, {}, name='bank-profile'),
url(r'^add-bank-profile/$', add_bank_profile, name='add-bank-profile'),
url(r'^my-services/$', my_services, name='my-services'),
url(r'^add-service/$', add_service, name='add-service'),
url(r'^add-gallery/$', add_gallery, name='add-gallery'),
url(r'^my-gallery/$', my_gallery, name='my-gallery'),
url(r'^add-gallery/$', add_instagram_pictures, name='add-instagram-pictures'),
]
settings.py:
# instagram settings
CLIENT_ID = "XXXXXXXXXXXXXXXXXXXX"
SECRET_ID = "XXXXXXXXXXXXXXXXXXXX"
How do I go about my use case as I have stated, my solution is in no way closer to what I want to do.
I am getting the following error
NoReverseMatch at /blog/index/
and
Reverse for 'feed_articles' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
just because I deleted/commented out a url I know longer need. The url isn't blog/index either. something tells me it's an ordering issue because I've had that happen before in my django app. But I am confused. Heres what I commented out
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^index/$', views.index, name='index'),
url(r'^video_submission/$', views.video_submission, name='submission'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^privacy/$', views.privacy, name='privacy'),
url(r'^dmca/$', views.dmca, name='dmca'),
url(r'^terms/$', views.terms, name='terms'),
url(r'^search/$', views.post_search, name='post_search'),
url(r'^create/$', views.post_create, name='create'),
url(r'^images/$', views.static_images, name='static_images'),
url(r'^video_agreement/$', views.video_agreement, name='video_agreement'),
### url(r'^feed_articles/$', views.articles_list, name='feed_articles'),
url(r'^feeds/$', views.feed_list, name='feed_list'),
url(r'^feeds/new$', views.new_feed, name='new_feed'),
url(r'^(?P<id>\d+)/feed_delete/$', views.feed_delete, name='feed_delete'),
url(r'^(?P<id>\d+)/article_delete/$', views.article_delete, name='article_delete'),
url(r'^tag/(?P<tag_slug>[-\w]+)/$', views.post_list,
name='post_list_by_tag'),
url(r'^(?P<slug>[\w-]+)/$', views.post_detail, name='post_detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', views.post_update, name='update'),
url(r'^(?P<id>\d+)/delete/$', views.post_delete, name='delete'),
How can I correct my syntax so that this minor issue will work?
EDIT my view being called by blog/index
{% extends 'blog/base.html' %}
{% load staticfiles %}
<style>
{% block style %}
#hite{
min-height: 720px;
}
.post{
min-height: 157px;
max-height: 157px;
width: 100%;
}
.tall{
height: 300px;
}
{% endblock style %}
</style>
{% block content %}
<div id="hite">
<h1>worldstar</h1>
{% for d in divs %}
<div class="col-sm-6 col-md-4" style="margin-top: 30px">
<div class="thumbnail thumb tall">
<img src="{{d.src}}" alt="{{ d.text }}" class="img-responsive post">
<div class="caption">
<h5>{{ d.text }}</h5>
<p></p>
<p>World *</p>
</div>
</div>
</div>
{% endfor %}
{% for a in articles %}
<div class="col-sm-6 col-md-4" style="margin-top: 30px" >
<div class="thumbnail thumb tall">
<div class="caption">
<h4 style="height: 100px">{{a.title}}</h4>
<p>{{a.description|truncatechars:30 | safe}}</p>
<h4>From: {{a.feed|truncatechars:30}}</h4>
<p>
View Article
{% if user.is_authenticated %}
delete
{% endif %}
</p>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
my feed_articles
{% extends 'blog/base.html' %}
<style>
{% block style %}
/*ul{*/
/*list-style: none;*/
/*}*/
#hi{
min-height: 720px;
margin-top: 15px;
}
{% endblock style %}
</style>
{% block jumbotron %}
<h1>HArticles</h1>
<p> Have a look around at some of the latest news Here and abroad</p>
{% endblock jumbotron %}
{% block content %}
<div class="row" id="hi">
{% for a in articles %}
<div class="col-xs-12 col-sm-4 col-md-3 col-lg-6" >
<div class="thumbnail" style="height: 250px; padding-left: 10px">
<h4 style="height: 100px">{{a.title}}</h4>
<p>{{a.description|truncatechars:30 | safe}}</p>
<h4>From: {{a.feed|truncatechars:30}}</h4>
<p>
View Article
{% if user.is_authenticated %}
delete
{% endif %}
</p>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
{% block aside %}
<h3 class="panel-body panel panel-default text-center"></h3>
{% endblock aside %}
Find the template that is being rendered by whichever view is being called when '/blog/index/' is requested, and look in that template for {% url 'path_to_this_urls_file:feed_articles' %}. Also look in any templates which that template extends and/or includes, including template tags.
I'm getting the below error and I have no idea why... I swear everything was working fine yesterday... I'm using python 3.5 and django 1.9. I went through most of the NOReverseMatch posts, but I couldn't find anything... please help
"Reverse for 'xxx' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
and
In template F:\Django-tutorial\src\landing_pages\templates\landing_pages\home.html, error at line 0
home.html should be displayed as a main page
My projects url.py:
urlpatterns = [
url(r'^', include('landing_pages.urls')),
url(r'^admin/', admin.site.urls),
url(r'^in/', include('web_monitor.urls')),
]
landing_pages.urls:
from django.conf.urls import url
from landing_pages.views import home, contact, about, cost
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^contact/$', contact, name='contact'),
url(r'^about/$', about, name='about'),
url(r'^cost/$', cost, name='cost'),
]
Home.html:
{% extends 'landing_pages/base.html' %}
{% load crispy_forms_tags %}
{% block jumbotron %}
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron">
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-3">
<form method="POST" action="">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Submit">
</form>
</div>
</div>
{% endblock %}
base.html:
{% load staticfiles %}
<html lang="en">
<head>
{% include 'landing_pages/head_css.html' %}
</head>
<body>
{% include 'landing_pages/nav_bar.html' %}
<div class="container">
{% block jumbotron %}
{% endblock %}
{% block content %}
{% endblock %}
</div> <!-- /container -->
</body>
</html>
nav_bar.html is just a nav bar, where i reference links like this:
<li>Cost</li>